SDNU_ACM_ICPC_2020_Winter_Practice_3rd A dfs

题目

Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audience of youngsters, placed around an m n field. Tom which is turned off initially is placed in some arbitrary point in the field by a volunteer from the audience. At time zero of the show, Tom is turned on by a remote control. Poor Tom is shown a holographic illusion of Jerry in a short distance such that a direct path between them is either vertical or horizontal. There may be obstacles in the field, but the illusion is always placed such that in the direct path between Tom and the illusion, there would be no obstacles. Tom tries to reach Jerry, but as soon as he gets there, the illusion changes its place and the chase goes on. Let’s call each chase in one direction (up, down, left, and right), a chase trip. Each trip starts from where the last illusion was deemed and ends where the next illusion is deemed out. After a number of chase trips, the holographic illusion no more shows up, and poor Tom wonders what to do next. At this time, he is signaled that for sure, if he returns to where he started the chase, a real Jerry is sleeping and he can catch it.

To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal or vertical. It’s assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.

The problem is that Tom’s logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom’s memory to help him go back. But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers m and n, which are the number of rows and columns of the grid respectively (1 <= m, n <= 100). Next, there are m lines, each containing n integers which are either 0 or 1, indicating whether the corresponding cell of the grid is empty (0) or occupied by an obstacle (1). After description of the field, there is a sequence of lines, each corresponding to a chase trip of Tom (in order). Each line contains two positive integers which together specify the range of steps Tom has taken (inclusive), followed by a single upper-case character indicating the direction of the chase trip, which is one of the four cases of R (for right), L (for left), U (for up), and D (for down). (Note that these directions are relative to the field and are not directions to which Tom turns). This part of the test case is terminated by a line containing exactly two zeros.
Output
For each test case, there should be a single line, containing an integer indicating the number of cells that Tom might have started the chase from.

大意
每个测试用例给出m*n的矩阵,1代表有障碍

接着输入tom追逐过程
两个数 a b 为tom走的步数范围,后跟一个字母表示方向
方向是相对矩阵的,不是tom的上下左右

如果所有的步子都能走完且在这个工程中不出界
也不碰到障碍物,就计数+1。求有多少条这样的可行路径

输出可能的路径总数

思路

直接dfs暴力求解

代码

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<cmath>
#include <queue>
using namespace std;
typedef long long ll;
int mp[110][110],n,m,T,num,ans;
int dx[]={0,0,1,-1},dy[]={-1,1,0,0};///左,右,下,上
struct mmm
{
    int le,mo,dir;
}mv[1000];
bool dfs(int x,int y,int co,int step)
{
    if(step==num)///操作数已够给定的操作数
    {
        return true;
    }
    if(co>=mv[step].le&&co<=mv[step].mo)///这次操作的已走步数在范围里
    {
        if(dfs(x,y,0,step+1))
            return true;
    }
    if(co<mv[step].mo)
    {
        int nx=x+dx[mv[step].dir],ny=y+dy[mv[step].dir];
        if(nx>=0&&nx<m&&ny>=0&&ny<n&&mp[nx][ny]==0)
            if(dfs(nx,ny,co+1,step))
                return true;
    }
    return false;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }///输地图
        num=0;///编号
        while(scanf("%d%d", &mv[num].le, &mv[num].mo),mv[num].le || mv[num].mo)
        {
            char f;
            cin>>f;
            if(f == 'L')
                mv[num++].dir = 0;
            else if(f == 'R')
                mv[num++].dir = 1;
            else if(f == 'D')
                mv[num++].dir = 2;
            else if(f == 'U')
                mv[num++].dir = 3;
        }///存每一次的步数范围,方向
        ans=0;
        for(int i = 0; i < m; i++)//开始遍历每一个点
            for(int j = 0; j < n; j++)
                if(mp[i][j] == 0 )
                    if(dfs(i, j, 0, 0))ans++;///起点无障碍
                        
        printf("%d\n", ans);
    }
    return 0;
}


发布了46 篇原创文章 · 获赞 0 · 访问量 1140

猜你喜欢

转载自blog.csdn.net/weixin_45719073/article/details/104321373