Robots on a grid

Description

You have recently made a grid traversing robot that can find its way from the top left corner of a grid to the bottom right corner. However, you had forgotten all your AI programming skills, so you only programmed your robot to go rightwards and downwards (that’s after all where the goal is). You have placed your robot on a grid with some obstacles, and you sit and observe. However, after a while you get tired of observing it getting stuck, and ask yourself “How many paths are there from the start position to the goal position?”, and “If there are none, could the robot have made it to the goal if it could walk upwards and leftwards?”
So you decide to write a program that, given a grid of size n×n with some obstacles marked on it where the robot cannot walk, counts the different ways the robot could go from the top left corner s to the bottom right t, and if none, tests if it were possible if it could walk up and left as well.

However, your program does not handle very large numbers, so the answer should begiven modulo 231-1.

Input

On the first line is one integer, 1 ≤ n ≤ 1000. Then follows n lines, each with n characters, where each character is one of ’.’ and ’#’, where ’.’ is to be interpreted as a walkable tile and ’#’ as a non-walkable tile. There will never be a wall at s, and there will never be a wall at t.

Output

Output one line with the number of different paths starting in s and ending in t (modulo 231-1.) or THE GAME IS A LIE if you cannot go from s to t going only rightwards and downwards but you can if you are allowed to go left and up as well, or INCONCEIVABLE if there simply is no path from s to t.

Sample Input

5
.....
#..#.
#..#.
...#.
.....
7
......#
####...
.#.....
.#....#
.#.....
.#..###
.#.....

Sample Output

6
THE GAME IS A LIE

这道题是一道搜索题,.表示能走,#表示不能走,就是条件有点多。

1.只能往前走不能后退,如果有多条路径能够从左上角的点走到右下角的话就输出路径的条数。

2.在不能后退的情况下没有路径,后退能找到路径就输出"THE GAME IS A LIE".

3.如果没有路径能到达就输出"INCONCEIVABLE".

就是先搜一遍,然后第二种情况就用DFS搜一次,就出来了。

#include<iostream>
#include <stdio.h>
#include<string.h>
using namespace std;
#define d 2147483647
char N[1111][1111];
long long  f[1111][1111],f1[1111][1111];
long long mx[5]={0,0,1,-1};
long long my[5]={1,-1,0,0};
long long n;

long long dfs(long long x,long long y)
{
    if(f1[x][y]!=-1) return f1[x][y];
    f1[x][y]=0;
    for(long long r=0;r<4;r++)
    {
        long long tx=x+mx[r];
        long long ty=y+my[r];
        if(N[tx][ty]=='.'&&f1[tx][ty]!=0&&tx>=0&&tx<n&&ty>=0&&ty<n)
            if(dfs(tx,ty)==1)
        {
            f1[x][y]=1;
            return 1;
        }
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
	long long i,j,k;
	while(scanf("%d",&n)!=EOF)
	//while(cin>>n)
	{
	    memset(f,0,sizeof(f));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                    cin>>N[i][j];
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
        {
            if(i==0&&j==0)  f[i][j]=1;
            else if(N[i][j]=='#')   f[i][j]=0;
            else if(i==0)   f[i][j]=f[i][j-1]%d;
            else if(j==0)   f[i][j]=f[i-1][j]%d;
            else if(i>0&&i<n&&j>0&&j<n)   f[i][j]=(f[i][j-1]%d+f[i-1][j]%d)%d;
        }
        if(f[n-1][n-1]!=0)  cout<<f[n-1][n-1]<<endl;
        else
        {
            memset(f1,255,sizeof(f1));
            f1[0][0]=1;
            f1[n-1][n-1]=dfs(n-1,n-1);
            if(f1[n-1][n-1]==1) cout<<"THE GAME IS A LIE"<<endl;
            else
               cout<<"INCONCEIVABLE"<<endl;
        }
	}
return 0;
}

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值