题目:https://ac.nowcoder.com/acm/contest/218#question
参考:http://www.cnblogs.com/acgoto/p/9972613.html
A
#include <bits/stdc++.h> using namespace std; vector<string> v; const int N=1e6+5; bool vis[N]; int ans,n,m; int d[2][4]={{-1,0,1,0},{0,1,0,-1}}; void dfs(int x,int y) { if (x<0||y<0||x>=n||y>=m||vis[x*m+y]==1||v[x][y]=='#')//x<0||y<0||x>=n||y>=m不能放在vis[x*m+y]==1之后!否则如x=-1时就会出错! { return; } ans++; vis[x*m+y]=true;//用一维标记数组的精华! v[x][y]='#'; for (int i=0;i<4;i++) { int xx=x+d[0][i]; int yy=y+d[1][i]; dfs(xx,yy); } } int main() { // freopen("in.txt","r",stdin); while (cin>>n>>m) { v.clear(); for (int i=0;i<n;i++) { string s; cin>>s; v.push_back(s); } ans=0; memset(vis,0,sizeof(vis)); for (int i=0;i<m;i++) { if (v[0][i]=='.') { dfs(0,i); } if (v[n-1][i]=='.') { dfs(n-1,i); } } for (int i=0;i<n;i++) { if (v[i][0]=='.') { dfs(i,0); } if (v[i][m-1]=='.') { dfs(i,m-1); } } cout<<n*m-ans<<endl; } return 0; }
B
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { int k; scanf("%d",&k); if(k==1) { printf("0 0 1 1 "); printf("0 1 1 0 "); } else { if(k%2==0) { for(int i=0; i<(k/2)-1; i++) { printf("%d %d %d %d ",0,3+2*i,2+2*i,0); printf("%d %d %d %d ",1+2*i,k,k,2+2*i); } printf("%d %d %d %d ",0,k,k,0); printf("%d %d %d %d ",0,0,k,1); printf("%d %d %d %d ",k-1,0,k,k); } else { for(int i=0;i<(k/2);i++) { printf("%d %d %d %d ",0,3+i*2,2+2*i,0); printf("%d %d %d %d ",0+2*i,k,k,1+2*i); } printf("%d %d %d %d ",0,0,k,1); printf("%d %d %d %d ",k-1,0,k,k); } } } return 0; }
遇图形题目,找规律时要认真画图