标签:cep panel dir queue void auth iostream out inpu
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1276 Accepted Submission(s): 466
/* * @Author: lyuc * @Date: 2017-08-12 14:12:48 * @Last Modified by: lyuc * @Last Modified time: 2017-08-13 21:50:49 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> #define LL long long #define INF 0x3f3f3f3f #define MAXN 105 using namespace std; char mapn[MAXN][MAXN]; int n,m; int res1; int res2; int dir[4][2]={1,0,-1,0,0,1,0,-1}; bool vis[MAXN][MAXN]; bool ok1(int x,int y){ if(x<0||x>=n||y<0||y>=m||vis[x][y]==true||mapn[x][y]==‘0‘) return false; return true; } void dfs1(int x,int y){ vis[x][y]=true; for(int i=0;i<4;i++){ int fx=x+dir[i][0]; int fy=y+dir[i][1]; if(ok1(fx,fy)==false) continue; dfs1(fx,fy); } } bool ok2(int x,int y){ if(x<0||x>n||y<0||y>m||vis[x][y]==true||mapn[x][y]==‘1‘) return false; return true; } void dfs2(int x,int y){ vis[x][y]=true; for(int i=0;i<4;i++){ int fx=x+dir[i][0]; int fy=y+dir[i][1]; if(ok2(fx,fy)==false) continue; dfs2(fx,fy); } } void init(){ res1=0; res2=0; memset(vis,false,sizeof vis); } int main(){ // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(scanf("%d%d",&n,&m)!=EOF){ init(); for(int i=1;i<=n;i++){ scanf("%s",mapn[i]+1); } n++; m++; for(int i=0;i<=n;i++){ mapn[i][0]=‘0‘; mapn[i][m]=‘0‘; } for(int i=0;i<=m;i++){ mapn[0][i]=‘0‘; mapn[n][i]=‘0‘; } //找1联通块 for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ if(mapn[i][j]==‘1‘){ if(vis[i][j]==true) continue; res1++; dfs1(i,j); } } } //找0联通快 memset(vis,false,sizeof vis); for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ if(mapn[i][j]==‘0‘){ if(vis[i][j]==true) continue; res2++; dfs2(i,j); } } } if(res1==1&&res2==1){ puts("1"); }else if(res1==1&&res2==2){ puts("0"); }else{ puts("-1"); } } return 0; }
标签:cep panel dir queue void auth iostream out inpu
原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/7355100.html