zoukankan      html  css  js  c++  java
  • bnu Robots on a grid

    Robots on a grid

    3000ms
    65536KB
     
    64-bit integer IO format: %lld      Java class name: Main
    Font Size:  
    Type:  

    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 nn 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 be given 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

    Sample Input 1
    5
    .....
    #..#.
    #..#.
    ...#.
    .....
    
    Sample Input 2
    7
    ......#
    ####...
    .#.....
    .#...#.
    .#.....
    .#..###
    .#.....
     

    Sample Output

    Sample Output 1
    6
    
    Sample Output 2
    THE GAME IS A LIE
    表示用dfs re到死 不过也表示自己太水了,re了7,8次还不知道换哎哎!
    先dp记录一下,如果有路就直接输出,否者bfs判断一下
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #define MOD 2147483647
    using namespace std;
    int vis[1005][1005],n;
    int dir[4][2]={-1,0,0,1,0,-1,1,0};
    long long dp[1005][1005];
    char map[1005][1005];
    struct node
    {
         int x,y;
    }info;
    bool bfs(int x,int y)
    {
         int i;
         info.x=x,info.y=y;
         queue<node>q;
         while(!q.empty()) q.pop();
         q.push(info);
         while(!q.empty())
         {
               node pos;
               pos=q.front();
               q.pop();
               if(dp[pos.x][pos.y]!=0)
                   return true;
               for(i=0;i<4;i++)
               {
                    int xx=pos.x+dir[i][0],yy=pos.y+dir[i][1];
                   if(xx>=0&&xx<n&&yy>=0&&yy<n&&!vis[xx][yy]&&map[xx][yy]!='#')
                   {
                        vis[xx][yy]=1;
                        info.x=xx;
                        info.y=yy;
                        q.push(info);
                   }
               }
         }
         return false;
    }
    int main()
    {
         int i,j;
         while(~scanf("%d",&n))
         {
              memset(vis,0,sizeof(vis));
              memset(dp,0,sizeof(dp));
              dp[0][0]=1;
              for(i=0;i<n;i++)
                    scanf("%s",map[i]);
              for(i=0;i<n;i++)
                   if(map[i][0]!='#')  dp[i][0]=1;
                   else  break;
              for(i=0;i<n;i++)
                   if(map[0][i]!='#') dp[0][i]=1;
                   else break;
              for(i=1;i<n;i++)
                for(j=1;j<n;j++)
                        if(map[i][j]!='#')
                        dp[i][j]=(dp[i][j-1]%MOD+dp[i-1][j]%MOD)%MOD;
            if(dp[n-1][n-1]==0)
            {
                 vis[n-1][n-1]=1;
                   if( bfs(n-1,n-1)) printf("THE GAME IS A LIE
    ");
                   else printf("INCONCEIVABLE
    ");
            }
            else printf("%lld
    ",dp[n-1][n-1]);
         }
         return 0;
    }
  • 相关阅读:
    JAVA中拼音工具类
    USB接口不同颜色的作用
    java的反射机制使用方法
    利用FrameLayout+LinearLayout实现Android底部导航栏切换
    Andorid中如何读取文件
    JAVA中解压压缩包到制定文件夹工具方法
    Andorid中子线程更新主线程方法
    委托的定义使用
    Json的序列化和反序列化
    在线支付(模拟支付宝)
  • 原文地址:https://www.cnblogs.com/llei1573/p/3285976.html
Copyright © 2011-2022 走看看