深搜,从一点向各处搜找到全部能走的地方。

Problem Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on
black tiles.



Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.



There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.



'.' - a black tile

'#' - a red tile

'@' - a man on a black tile(appears exactly once in a data set)

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

Source

Asia 2004, Ehime (Japan), Japan Domestic 



代码:


#include<iostream>
using namespace std;
char map[22][22];//定义最大数组
int sum,l,h;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //四个方位,上、下、左、右
bool border(int x,int y)//推断是否超范围
{
if(x<0||x>=h||y<0||y>=l) return 0;
return 1;
}
void search(int x,int y)
{
int i;
int xx,yy;
sum++;//记录长度
map[x][y]='#';//标记为已走
for(i=0;i<4;i++) //以当前位置向四个方向扩展
{
xx=x+dir[i][0];
yy=y+dir[i][1];
if(border(xx,yy)&&map[xx][yy]=='.') //满足条件就以当前位置继续扩展
search(xx,yy);
}
}
int main()
{
int i,j;
int x0,y0;
while(cin>>l>>h)
{
sum=0;
if(l==0&&h==0)break;
for(i=0;i<h;i++)
{
for(j=0;j<l;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')//记录当前位置
{
x0=i;
y0=j;
}
}
}
search(x0,y0);//调用当前位置
cout<<sum<<endl;
}
return 0;
}


A Knight's Journey


Description

Background 

The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey 

around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board,
but it is still rectangular. Can you help this adventurous knight to make travel plans? 



Problem 

Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

Input

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes
how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves
followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number. 

If no such path exist, you should output impossible on a single line.

Sample Input

3
1 1
2 3
4 3

Sample Output

Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4

Source

TUD Programming Contest 2005, Darmstadt, Germany

八个方向的深搜回溯 把移动方向打好 (网上好多人说要按字典序走才干A   測试了一下  不按字典序也A了)

代码:

#include<iostream>
#include<cstring>
#define M 30
int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int dy[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
using namespace std;
int cas,n,m,tag;
int map[M][M],t;
char ans[900][2];
void dfs(int x,int y,int k)
{
int xx,yy,i,j;
if(k==n*m)
{
tag=1;
}
for(i=0;i<8;i++)
{
xx=x+dx[i];
yy=y+dy[i];
if(map[xx][yy]==0&&xx>0&&xx<=n&&yy>0&&yy<=m)
{
ans[k][0]=ans[k-1][0]+dy[i];//注意这里x轴移动的位移并非字母轴的位移而是数字轴的位移。。坑我好久
ans[k][1]=ans[k-1][1]+dx[i];
map[xx][yy]=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
cout<<map[i][j];cout<<endl;
}cout<<endl;
dfs(xx,yy,k+1);
if(tag)
return ;
map[xx][yy]=0;
}
}
//return ;
}
int main()
{
int i,j,l=1;
cin>>cas;
while(l<=cas)
{
memset(map,0,sizeof(map));
memset(ans,'0',sizeof(ans));
map[1][1]=1;
ans[0][0]='A';
ans[0][1]='1';
cin>>n>>m;
t=1;
tag=0;
cout<<"Scenario #"<<l<<":"<<endl;
dfs(1,1,1);
if(tag)
{
//cout<<ans[0][0]<<ans[0][1];
for(i=0;i<n*m;i++)
cout<<ans[i][0]<<ans[i][1];
}
else
cout<<"impossible";
cout<<endl<<endl;
l++;
}
}

DFS深搜——Red and Black——A Knight&#39;s Journey的更多相关文章

  1. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  2. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

  3. Red and Black(DFS深搜实现)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  4. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  6. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  7. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

  8. CCF 模拟E DFS深搜

    http://115.28.138.223:81/view.page?opid=5 这道题问的很怪. 起点DFS,每一个点还要DFS一次,统计不能到终点的个数 数据量不大这样做也能AC #includ ...

  9. HDU_1241 Oil Deposits(DFS深搜)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

随机推荐

  1. BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间( dp )

    水dp 先按开始时间排序 , 然后dp. dp( i ) 表示前 i 个时间段选第 i 个时间段的最优答案 , 则 dp( i ) = max( dp( j ) ) + w_i ( 0 < j ...

  2. Mac Yosemite下Android Studio环境问题集合

    1. java not found 在mac Yosemite下,因jre升级到1.8,导致Android Studio无法启动.报错:"JAVA not found". 解决方法 ...

  3. [LeetCode]题解(python):012-Integer to Roman

    题目来源: https://leetcode.com/problems/integer-to-roman/ 题意分析: 这道题是要把在区间[1-3999]的数字转化成罗马数字. 题目思路: 只要知道了 ...

  4. 我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)

    os模块 提供对操作系统进行调用的接口 >>> import os >>> os.getcwd() # 获取当前工作目录,类似linux的pwd命令 '/data/ ...

  5. AutoCAD 2014简体中文版官方正式版x86 x64下载,带注册机,永久免费使用

    注册机使用说明:会有部分杀毒软件报病毒,请无视.操作步骤:1.安装Autodesk AutoCAD 20142.使用以下序列号666-69696969安装.3.产品密码为001F14.安装完成后,启动 ...

  6. EAN 通用商品条形码

    商品条形码是指由一组规则排列的条.空及其对应字符组成的标识,用以表示一定的商品信息的符号.其中条为深色.空为纳色,用于条形码识读设备的扫描识读.其对应字符由一组阿拉伯数字组成,供人们直接识读或通过键盘 ...

  7. QStringLiteral的两篇外文解释(编译期转换成QString)

    http://blog.qt.io/blog/2014/06/13/qt-weekly-13-qstringliteral/ https://woboq.com/blog/qstringliteral ...

  8. .Net之路(十三)数据库导出到EXCEL

    .NET中导出到Office文档(word,excel)有我理解的两种方法.一种是将导出的文件存放在server某个目录以下,利用response输出到浏览器地址栏,直接打开:还有直接利用javasc ...

  9. IOS中使用像素位图(CGImageRef)对图片进行处理

    IOS中对图片进行重绘处理的方法总结 一.CGImageRef是什么 CGImageRef是定义在QuartzCore框架中的一个结构体指针,用C语言编写.在CGImage.h文件中,我们可以看到下面 ...

  10. Python之路Day10

    本节主要内容:memcache&redis.RabbitMQ.twisted框架 1. memcache&redis 1.1 memcache Memcached 是一个高性能的分布式 ...