赞
踩
7.12
#include<bits/stdc++.h> using namespace std; bool _m[51][51]; bool _flag[2600]; bool _path[51][51]; struct { int x; int y; } order[2600]; int p,q; int n,m; int minn=2600; int _next[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int step, int x, int y) { if(x==p-1&&y==q-1) { if(step<minn) minn=step; _path[0][0]=1; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(_m[i][j]) { printf("@ "); } else { if(i==p-1&&j==q-1) { printf("# "); } else { printf("%d ",_path[i][j]); } } } putchar('\n'); } putchar('\n'); for(int i=0; i<step; i++) { printf("%d %d\n",order[i].x,order[i].y); } putchar('\n'); putchar('\n'); } int tmpx,tmpy; for(int i=0; i<4; i++) { tmpx=x+_next[i][0]; tmpy=y+_next[i][1]; if(!_path[tmpx][tmpy]&&(tmpx>=0&&tmpx<n)&&(tmpy>=0&&tmpy<m)&&!_m[tmpx][tmpy]) { _path[tmpx][tmpy]=1; order[step].x=tmpx; order[step].y=tmpy; dfs(step+1,tmpx,tmpy); _path[tmpx][tmpy]=0; } } return ; } int main() { order[0].x=0; order[0].y=0; while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { scanf("%d",&_m[i][j]); } } scanf("%d%d",&p,&q); dfs(1,0,0); printf("way = %d\n",minn); } return 0; } /* 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 4 3 10 10 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 6 6 */
7.13
#include<bits/stdc++.h> using namespace std; bool _m[51][51]; bool _flag[2600]; int _path[51][51]; struct { int x; int y; } order[2600]; int p,q; int n,m; int minn=2600; int _next[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int step, int x, int y) { if(x==p-1&&y==q-1) { if(step<minn) minn=step; _path[0][0]=1; putchar('\n'); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(_m[i][j]) { printf("@ "); } else { if(i==p-1&&j==q-1) { printf("# "); } else { printf("%d ",_path[i][j]); } } } putchar('\n'); } putchar('\n'); for(int i=0; i<step; i++) { printf("%d %d\n",order[i].x,order[i].y); } putchar('\n'); putchar('\n'); } int tmpx,tmpy; for(int i=0; i<4; i++) { tmpx=x+_next[i][0]; tmpy=y+_next[i][1]; if(!_path[tmpx][tmpy]&&(tmpx>=0&&tmpx<n)&&(tmpy>=0&&tmpy<m)&&!_m[tmpx][tmpy]) { _path[tmpx][tmpy]=step+1; order[step].x=tmpx; order[step].y=tmpy; dfs(step+1,tmpx,tmpy); _path[tmpx][tmpy]=0; } } return ; } int main() { order[0].x=0; order[0].y=0; while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { scanf("%d",&_m[i][j]); } } scanf("%d%d",&p,&q); dfs(1,0,0); printf("way = %d\n",minn); } return 0; } /* 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 4 3 10 10 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 6 6 */
#include<bits/stdc++.h> using namespace std; bool _m[51][51]; bool _flag[2600]; int _path[51][51]; struct { int x; int y; } order[2600]; int p,q; int n,m; int minn=2600; int _next[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int step, int x, int y) { if(x==p-1&&y==q-1) { if(step<minn) minn=step; _path[0][0]=1; printf("\n------------------------\n"); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(_m[i][j]) printf("@ "); else { if(i==p-1&&j==q-1) printf("# "); else printf("%d ",_path[i][j]); } } putchar('\n'); } putchar('\n'); for(int i=0; i<step; i++) printf("%d %d\n",order[i].x,order[i].y); printf("------------------------\n"); } int tmpx,tmpy; for(int i=0; i<4; i++) { tmpx=x+_next[i][0]; tmpy=y+_next[i][1]; if(!_path[tmpx][tmpy]&&(tmpx>=0&&tmpx<n)&&(tmpy>=0&&tmpy<m)&&!_m[tmpx][tmpy]) { _path[tmpx][tmpy]=step+1; order[step].x=tmpx; order[step].y=tmpy; dfs(step+1,tmpx,tmpy); _path[tmpx][tmpy]=0; } } return ; } int main() { order[0].x=0; order[0].y=0; while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { scanf("%d",&_m[i][j]); } } scanf("%d%d",&p,&q); dfs(1,0,0); printf("way = %d\n",minn-1); } return 0; } /* 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 4 3 10 10 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 6 6 */
8.15
#include<bits/stdc++.h> using namespace std; const int N=1000; int n,m; int x,y; int _m[N][N]; int _path[N][N]; int _way[N][2]; int idx=1; int _next[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; void dfs(int a, int b, int step) { if(a==x-1&&b==y-1) { cout<<"\n"; cout<<"\n------------------------\n"; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(_m[i][j]==1) cout<<"@ "; else { if(i==x-1&&j==y-1) cout<<"# "; else cout<<_path[i][j]<<" "; } } cout<<"\n"; } cout<<"\n"; for(int i=0;i<step;i++) cout<<_way[i][0]+1<<" "<<_way[i][1]+1<<"\n"; cout<<"\n------------------------\n"; return ; } int tmpa, tmpb; for(int i=0; i<4; i++) { tmpa=a+_next[i][0]; tmpb=b+_next[i][1]; if(tmpa<0||tmpb<0||tmpa>=n||tmpb>=m) continue; if(!_path[tmpa][tmpb]&&!_m[tmpa][tmpb]) { _path[tmpa][tmpb]=step+1; _way[step][0]=tmpa; _way[step][1]=tmpb; dfs(tmpa, tmpb, step+1); _path[tmpa][tmpb]=0; } } return ; } int main() { cin>>n>>m; for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>_m[i][j]; cin>>x>>y; _path[0][0]=1; dfs(0,0,1); return 0; } /* 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 4 3 */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。