标签:des blog http io ar os sp for java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 621 Accepted Submission(s): 254
Case n: Island splits when ocean rises f feet.or
Case n: Island never splits.Our convention here is if your answer is, say, 5 feet, you more accurately mean “5 feet plus a little more.” That is, at least a little water will be flowing over the originally 5 foot high portion of land.
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<string> #include<queue> #include<vector> #include<set> using namespace std; const int MAX=105; int grid[MAX][MAX]; bool vis[MAX][MAX]; int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int n,m; int max(int a,int b) { return a>b?a:b; } void dfs(int i,int j,int h) { if(i<1||j<1||i>n||j>m||vis[i][j]||grid[i][j]>h) return; vis[i][j]=true; grid[i][j]=0; for(int k=0;k<4;k++) dfs(i+d[k][0],j+d[k][1],h); } void dfs2(int i,int j) { if(i<1||j<1||i>n||j>m||vis[i][j]||(!grid[i][j])) return; vis[i][j]=true; for(int k=0;k<4;k++) dfs2(i+d[k][0],j+d[k][1]); } bool check(int h) { int i,j,cnt=0; memset(vis,0,sizeof(vis)); for(i=1;i<=m;i++) if(!vis[1][i]) dfs(1,i,h); for(i=1;i<=n;i++) if(!vis[i][1]) dfs(i,1,h); for(i=1;i<=m;i++) if(!vis[n][i]) dfs(n,i,h); for(i=1;i<=n;i++) if(!vis[i][m]) dfs(i,m,h); memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(!vis[i][j]&&grid[i][j]) { cnt++; dfs2(i,j); } if(cnt<=1) return 1; else return 0; } int main() { int ca=0,i,j,maxx=-1,cnt,ans; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; cnt=0; ans=-1; memset(grid,0,sizeof(grid)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { scanf("%d",&grid[i][j]); maxx=max(maxx,grid[i][j]); } for(i=1;i<=maxx;i++) if(!check(i)) { ans=i; break; } printf("Case %d: ",++ca); if(ans==-1) printf("Island never splits.\n"); else printf("Island splits when ocean rises %d feet.\n",ans); } return 0; }
标签:des blog http io ar os sp for java
原文地址:http://www.cnblogs.com/a972290869/p/4164032.html