题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821

思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时候稍微注意一下就可以了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std; int n,m,cnt;
char str[];
int map[][];
int dir[][]={{-,},{,},{,-},{,}};
char Dir[]={'U','D','L','R'};
char path[]; bool Judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m)
return true;
return false;
} bool dfs(int x,int y,int len)
{
if(len>=cnt){
path[len]=;
return true;
}
for(int i=;i<;i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(!Judge(xx,yy)||map[xx][yy])continue;
while(Judge(xx,yy)&&!map[xx][yy]){
xx+=dir[i][];
yy+=dir[i][];
}
if(!Judge(xx+dir[i][],yy+dir[i][]))continue;
int tmp=map[xx][yy];
map[xx+dir[i][]][yy+dir[i][]]+=tmp-;
map[xx][yy]=;
path[len]=Dir[i];
if(dfs(xx,yy,len+))return true;
map[xx+dir[i][]][yy+dir[i][]]-=tmp-;
map[xx][yy]=tmp;
}
return false;
} int main()
{
while(~scanf("%d%d",&m,&n)){
cnt=;
for(int i=;i<n;i++){
scanf("%s",str);
for(int j=;j<m;j++){
if(str[j]!='.'){
cnt+=str[j]-'a'+;
map[i][j]=str[j]-'a'+;
}else
map[i][j]=;
}
}
bool flag=false;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(map[i][j]==&&dfs(i,j,)){ //必须从map[i][j]==0的位置开始
flag=true;
printf("%d\n%d\n",i,j);
cout<<path<<endl;
break;
}
}
if(flag)break;
}
}
return ;
}

hdu 2821(dfs)的更多相关文章

  1. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. hdu 2821 Pusher (dfs)

    把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀 ...

  4. hdu 2821 Pusher(dfs)

    Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...

  5. hdu 2821 Pusher (dfs)

    Pusher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)Total Subm ...

  6. hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...

  7. HDU 2821 Pusher

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 首先,题目描述给的链接游戏很好玩,建议先玩几关,后面越玩越难,我索性把这道题A了,也就相当于通关 ...

  8. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  9. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

随机推荐

  1. 携程实时大数据平台演进:1/3 Storm应用已迁到JStorm

    携程大数据平台负责人张翼分享携程的实时大数据平台的迭代,按照时间线介绍采用的技术以及踩过的坑.携程最初基于稳定和成熟度选择了Storm+Kafka,解决了数据共享.资源控制.监控告警.依赖管理等问题之 ...

  2. 设置linux中tcp默认的20秒connect超时时间(转)

    无论你用任何语言或者是网络库,你都可以设置网络操作的超时时间,特别是connect.read.write的超时时间. 你可以在代码中把超时时间设置任意大小值,但是connect方法会有一点特殊. co ...

  3. iOS8的一些控件的变更

    UISearchDisplayController变更为UISearchController UIAlertView变更为UIAlertController 如果添加点击事件则需要使用UIAlertC ...

  4. checkboxlist 横向显示,自动换行

    属性RepeatDirection 设为Horizontal RepeatColumns设置一个数字,表示每行显示几项 如果不想让每行显示的项是固定的,那么把RepeatLayout属性置为Flow

  5. C#:数学运算(待补充)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyCo ...

  6. C数组逆序

    一.标准交换模式 /**** *标准交换模式 *实现数组的逆序,原理就是数组的首尾元素进行交换 ***/ #define N 5; int main(){ int array[N] = {15,20, ...

  7. Atitit.软件GUI按钮与仪表盘(01)--报警系统--

    Atitit.软件GUI按钮与仪表盘(01)--报警系统-- 1. 温度报警区(鲁大师,360taskman) 1 2. os-区-----cpu_mem_io资源占用监测 1 3. Vm区 1 4. ...

  8. prompt() 方法

    定义和用法 prompt() 方法用于显示可提示用户进行输入的对话框. 语法 prompt(text,defaultText) 参数 描述 text 可选.要在对话框中显示的纯文本(而不是 HTML ...

  9. tableview 与 tableview cell

    1.tableview cell: import Foundationimport UIKit class CjwtCell: UITableViewCell { @IBOutlet var lb_c ...

  10. Project Euler:Problem 34 Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...