hdu 2821(dfs)
来源:互联网 发布:宋慈 知乎 编辑:程序博客网 时间:2024/06/07 12:54
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821
思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时候稍微注意一下就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<string> 6 using namespace std; 7 8 int n,m,cnt; 9 char str[33];10 int map[33][33];11 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};12 char Dir[7]={'U','D','L','R'};13 char path[1111];14 15 bool Judge(int x,int y)16 {17 if(x>=0&&x<n&&y>=0&&y<m)18 return true;19 return false;20 }21 22 bool dfs(int x,int y,int len)23 {24 if(len>=cnt){25 path[len]=0;26 return true;27 }28 for(int i=0;i<4;i++){29 int xx=x+dir[i][0];30 int yy=y+dir[i][1];31 if(!Judge(xx,yy)||map[xx][yy])continue;32 while(Judge(xx,yy)&&!map[xx][yy]){33 xx+=dir[i][0];34 yy+=dir[i][1];35 }36 if(!Judge(xx+dir[i][0],yy+dir[i][1]))continue;37 int tmp=map[xx][yy];38 map[xx+dir[i][0]][yy+dir[i][1]]+=tmp-1;39 map[xx][yy]=0;40 path[len]=Dir[i];41 if(dfs(xx,yy,len+1))return true;42 map[xx+dir[i][0]][yy+dir[i][1]]-=tmp-1;43 map[xx][yy]=tmp;44 }45 return false;46 }47 48 49 50 int main()51 {52 while(~scanf("%d%d",&m,&n)){53 cnt=0;54 for(int i=0;i<n;i++){55 scanf("%s",str);56 for(int j=0;j<m;j++){57 if(str[j]!='.'){58 cnt+=str[j]-'a'+1;59 map[i][j]=str[j]-'a'+1;60 }else 61 map[i][j]=0;62 }63 }64 bool flag=false;65 for(int i=0;i<n;i++){66 for(int j=0;j<m;j++){67 if(map[i][j]==0&&dfs(i,j,0)){ //必须从map[i][j]==0的位置开始68 flag=true;69 printf("%d\n%d\n",i,j);70 cout<<path<<endl;71 break;72 }73 }74 if(flag)break;75 }76 }77 return 0;78 }
0 0
- hdu 2821 dfs
- hdu 2821 DFS
- DFS-hdu-2821-Pusher
- hdu 2821(dfs)
- HDU-2821-Pusher(DFS)
- HDU 2821 DFS
- HDU 2821 DFS
- hdu 2821 Pusher DFS
- hdu 2821 Pusher (dfs)
- hdu 2821 Pusher(dfs)
- hdu 2821 Pusher (水dfs)
- 【搜索】 HDU 2821 Pusher DFS
- HDU 2821Pusher(DFS )
- HDU DFS
- 【DFS】HDU
- HDU DFS
- HDU-2610 && HDU-2610 (dfs)
- HDU 1864 (DFS)
- poj 1129(dfs+图的四色定理)
- poj 2531(dfs)
- 一个tcp校验码的错误。
- poj 1088(记忆化搜索)
- hdu 4294(bfs)
- hdu 2821(dfs)
- 为什么long和int都是4字节
- hdu 2437(dfs)
- hdu 3085(双向bfs)
- hdu 2686(状压dp)
- hdu 3599(最短路+最大流)
- php5.3.x连接sql server 2005 (php5.3连接mssql)
- hdu 3667(拆边+最小费用最大流)
- hdu 1011(树形dp)