大佬们帮忙看一下,全爆时间了

P3956 [NOIP2017 普及组] 棋盘

希望更丰富的展现?使用[Markdown](https://www.luogu.org/wiki/show?name=%E5%B8%AE%E5%8A%A9%EF%BC%9Amarkdown)
by 氷芽川四糸乃 @ 2019-10-03 08:53:47


希望更丰富的展现?使用Markdown
by 休年年年 @ 2019-11-08 08:25:37


```cpp #include <bits/stdc++.h> using namespace std; queue <int>qx; queue <int>qy; const int dx[4]={0,0,-1,1}; const int dy[4]={-1,1,0,0}; bool v[1001][1001]; int n,m,ans,i,j,x,y,tx,ty,c,a[1001][1001]; void bfs(int x,int y) { v[x][y]=false; qx.push(x); qy.push(y); while(!qx.empty()) { int minn=3,minx=1,miny=1; for(i=0;i<4;i++) { tx=qx.front()+dx[i]; ty=qy.front()+dy[i]; int s; if(a[tx][ty]==a[x][y]) s=0; if(a[tx][ty]!=a[x][y]&&v[tx][ty]) s=1; if(v[tx][ty]==false) s=2; if(tx>0&&ty<0&&tx<=n&&ty<=n&&v[tx][ty]==true&&s<minn) { minn=s; minx=tx; miny=ty; } } ans+=minn; v[minx][miny]=false; qx.push(minx); qy.push(miny); qx.pop(); qy.pop(); } } int main() { scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&c); a[x][y]=a[y][x]=c; v[x][y]=v[y][x]=true; } bfs(1,1); printf("%d\n",ans); return 0; } ```
by yzc358230151 @ 2019-11-09 15:44:38


|