标签:dfs class step log amp point for str clu
1 #include<cstdio> 2 int n,m,x,y,k; 3 struct Point{ 4 int x,y; 5 }st,ed; 6 bool vis[10][10],flag; 7 int dx[4]={0,-1,0,1}; 8 int dy[4]={1,0,-1,0}; 9 bool is_in(int x,int y) 10 { 11 if(1<=x && x<=n && 1<=y && y<=m) return 1; 12 else return 0; 13 } 14 bool judge(Point now) 15 { 16 Point next; 17 int cnt=0; 18 for(int i=0;i<4;i++) 19 { 20 next.x=now.x+dx[i]; 21 next.y=now.y+dy[i]; 22 if(is_in(next.x,next.y) && vis[next.x][next.y]) cnt++; 23 } 24 if(cnt<=1) return 1; 25 else return 0; 26 } 27 void dfs(Point now,int step) 28 { 29 if(flag) return; 30 if(now.x==ed.x && now.y==ed.y) 31 { 32 if(step>=k) 33 { 34 flag=1; 35 vis[now.x][now.y]=1; 36 } 37 return; 38 } 39 vis[now.x][now.y]=1; 40 Point next; 41 for(int i=0;i<4;i++) 42 { 43 next.x=now.x+dx[i]; 44 next.y=now.y+dy[i]; 45 if(is_in(next.x,next.y)&&!vis[next.x][next.y]&&judge(next)) 46 { 47 dfs(next,step+1); 48 if(flag==0) vis[next.x][next.y]=0; 49 } 50 } 51 return; 52 } 53 int main() 54 { 55 scanf("%d%d%d%d%d",&n,&m,&x,&y,&k); 56 st.x=1, st.y=1; 57 ed.x=x, ed.y=y; 58 flag=0; 59 for(int i=0;i<=9;i++) 60 { 61 for(int j=0;j<=9;j++) 62 { 63 if(is_in(i,j)) vis[i][j]=0; 64 else vis[i][j]=1; 65 } 66 } 67 dfs(st,0); 68 for(int i=1;i<=n;i++) 69 { 70 for(int j=1;j<=m;j++) 71 { 72 printf("%c",(vis[i][j]==1)?‘.‘:‘*‘); 73 } 74 printf("\n"); 75 } 76 }
标签:dfs class step log amp point for str clu
原文地址:http://www.cnblogs.com/dilthey/p/6865721.html