题目链接: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. Linux下的进程间通信-详解

     详细的讲述进程间通信在这里绝对是不可能的事情,而且笔者很难有信心说自己对这一部分内容的认识达到了什么样的地步,所以在这一节的开头首先向大家推荐著 名作者Richard Stevens的著名作品:&l ...

  2. DP SRM 661 Div2 Hard: ColorfulLineGraphsDiv2

    Problem Statement Bob is going to create a graph with N nodes. The graph will be constructed in two ...

  3. 流媒体协议RTMP,RTSP与HLS有什么不同

    转载自:http://www.cuplayer.com/player/PlayerCode/Wowza/2015/0204/1774.html HLS (HTTP Live Streaming) Ap ...

  4. mosquitto配置文件详解

    安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明. # Config f ...

  5. GridControl摘录

    gvCabTotalInfo.Columns["出线平均<br>电压"].SummaryItem.SummaryType = DevExpress.Data.Summa ...

  6. django abstract base class ---- 抽象基类

    抽象蕨类用于定义一些同享的列.类本身并不会在数据库端有表与之对应 一.例子: 1.定义一个叫Person 的抽象基类.Student 继承自Person from django.db import m ...

  7. 实现编程时Vim自动导入相应模板

    Vim文本编辑器以简洁高效著称,那么我们在编程时能有自动加载相应的模板,从而省去一些固定的输入提升工作效率呢!当然可以,可以有多种方法实现,我这里介绍一种非常简单的方法. 首先在你的主用户文件下面建立 ...

  8. sqlzoo练习答案--SUM and COUNT

    World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, ...

  9. cocos2d-x 之 CCProgressTimer

    --绕圆心转动的进度动画 local function SpriteProgressToRadial() local leftProgress = CCProgressTimer:create(CCS ...

  10. shell判断文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...