题目链接:
POJ:http://poj.org/problem?id=1071
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1364
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=19
Description
Input
Output
Sample Input
2 6 6 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 2 R 1 2 D 1 1 R 0 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0 1 2 R 3 7 U 0 0
Sample Output
10 0
Source
题意:
给出一个m*n的地图,给出机器人的一系列动作,机器人可以从任意的一点为起点,求机器人可能的起点个数;
每个动作由一个范围和一个字母组成,字母表示机器人走的方向,范围表示走的步长.
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct
{
int l, r;
char dir;
} node;
node op[100017];
int mapp[117][117];
int n, m;
int flag = 0;
int step;
int movee(int &x, int &y, char dir)
{
if(dir == 'U')
{
if(x-1>=0 && mapp[x-1][y] == 0)
{
x--;
return 1;
}
return 0;
}
else if(dir == 'D')
{
if(x+1 < m && mapp[x+1][y] == 0)
{
x++;
return 1;
}
return 0;
}
else if(dir == 'L')
{
if(y-1>=0 && mapp[x][y-1] == 0)
{
y--;
return 1;
}
return 0;
}
else if(dir == 'R')
{
if(y+1<n && mapp[x][y+1] == 0)
{
y++;
return 1;
}
return 0;
}
}
void DFS(int x, int y, int k)
{
if(mapp[x][y] == 1)
return;//起始位置不能是1;
if(x<0 || x>=m || y<0 || y>=n)
return ;//不在图内;
if(k == step)
{
flag = 1;
return ;
}
int i;
for(i = 1; i < op[k].l; i++)
{
if(!movee(x,y,op[k].dir))
return ;
}
for(; i <= op[k].r; i++)
{
if(movee(x,y,op[k].dir))
{
DFS(x,y,k+1);
if(flag)
return ;
}
else
break;
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
int a, b;
char tt;
while(t--)
{
scanf("%d%d",&m,&n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d",&mapp[i][j]);
}
}
step = 0;
while(1)
{
scanf("%d%d",&a,&b);
if(a==0 && b==0)
break;
getchar();
scanf("%c",&tt);
op[step].l = a;
op[step].r = b;
op[step++].dir = tt;
}
int ans = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
flag = 0;
DFS(i,j,0);
if(flag)
ans+=1;
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 1071 & HDU 1364 & ZOJ 1019 Illusive Chase(DFS)
原文地址:http://blog.csdn.net/u012860063/article/details/39737839