博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSUFT 1002 Robot Navigation
阅读量:5104 次
发布时间:2019-06-13

本文共 6024 字,大约阅读时间需要 20 分钟。

1002: Robot Navigation

Time Limit: 1 Sec      Memory Limit: 128 MB
Submit: 4      Solved: 2

Description

A robot has been sent to explore a remote planet. To specify the path the robot should take, a program is sent each day. The program consists of a sequence of the following commands:

  • FORWARD: move forward by one unit.
  • TURN LEFT: turn left by 90 degrees. The robot remains at the same location.
  • TURN RIGHT: turn right by 90 degrees. The robot remains at the same location.

 

The robot also has sensor units which allows it to obtain a map of its surrounding area. The map is represented as a grid ofMrows andNcolumns. Each grid point is represented by a coordinate (r,c) wherer = 0is the north edge of the map,r = M - 1is the south edge,c = 0is the west edge, andc = N - 1is the east edge. Some grid points contain hazards (e.g. craters) and the program must avoid these points or risk losing the robot.

Naturally, if the initial location and direction of the robot and its destination position are known, we wish to send the shortest program (one consisting of the fewest commands) to move the robot to its destination (we do not care which direction it faces at the destination). You are more interested in knowing the number of different shortest programs that can move the robot to its destination, because we may need to send different sequences as interplanetary communication is not necessarily reliable. However, the number of shortest programs can be very large, so you are satisfied to compute the number as a remainder under some modulus, knowing that something you learned in classes called the Chinese remainder theorem can be used to compute the final answer.

Input

The input consists of a number of cases. The first line of each case gives three integersM,N, and the modulusm(0 < M, N <= 1000, 0 < m <= 1000000000). The nextMlines containNcharacters each and specify the map. A '.' indicates that the robot can move into that grid point, and a '*' indicates a hazard. The final line gives four integersr1,c1,r2,c2followed by a characterd. The coordinates (r1,c1) specify the initial position of the robot, and (r2,c2) specify the destination. The character d is one of 'N', 'S', 'W', 'E' indicating the initial direction of the robot. It is assumed that the initial position and the destination are not hazards.

The input is terminated whenm = 0.

Output

For each case, print its case number, the modulus, as well as the remainder of the number of different programs when divided by the modulusm. The output of each case should be on a single line, in the format demonstrated below. If there is no program that can move the robot to its destination, output -1 for the number of different programs.

Sample Input

3 3 100***.*.***1 0 1 2 E4 4 100*****.*.*.*.*...1 1 1 3 N4 8 100********...**...*......*********1 0 1 7 E0 0 0

Sample Output

Case 1: 100 -1Case 2: 100 2Case 3: 100 4

HINT

Source

 
最短路的条数。
我打算首先BFS看是否能够到,然后不标记路径BFS,这样的确可以得到答案,但是方法肯定不好。
正解:
增加一个数组 sum[][][4] 到状态的方案数,在最短路中当我有不同的方案时,来到该点的之前的 u ,和 要到的点的 v,步数 d 一定是相差 1的 ,这个时候,这个状态的方案数 + 1; 要是这个状态没有其他解,那么,sum(v) = sum(u),最后查看,终点的4个方向的 sum.
#include 
using namespace std; const char* dirs = "NESW";const int Maxn = 1005;const int INF = 0x3f3f3f3f; int R,C;int mod; char a[Maxn][Maxn]; struct Node{ int r,c; int dir; Node(int r=0,int c=0,int dir=0):r(r),c(c),dir(dir) {}}; const int dr[] = {-1,0,1,0};const int dc[] = {
0,1,0,-1}; Node walk(const Node& u,int turn){ int dir = u.dir; if(turn==0) dir = (dir - 1 + 4)%4; // zuo zhuan if(turn==2) dir = (dir+ 1)%4; // you zhuan if(turn==1) return Node(u.r+dr[dir],u.c+dc[dir],dir); // zhi zou return Node(u.r,u.c,dir);} int d[Maxn][Maxn][4];int sum[Maxn][Maxn][4]; int dir_id(char c){ return strchr(dirs,c)-dirs;} int r1,c1,r2,c2,dir; bool inside(int r,int c){ if(r>=0&&r
=0&&c
q; memset(d,-1,sizeof(d)); Node u(r1,c1,dir); d[u.r][u.c][u.dir] = 0; sum[u.r][u.c][u.dir] = 1; q.push(u); cnt = 0; while(!q.empty()) { Node u = q.front(); q.pop(); for(int i=0; i<3; i++) { Node v = walk(u,i); if(a[v.r][v.c]=='.'&&inside(v.r,v.c)&&d[v.r][v.c][v.dir]<0) { d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + 1; sum[v.r][v.c][v.dir] = sum[u.r][u.c][u.dir]; q.push(v); } else if(a[v.r][v.c]=='.'&&inside(v.r,v.c)) { if(d[v.r][v.c][v.dir]==d[u.r][u.c][u.dir]+1) { sum[v.r][v.c][v.dir] = (sum[v.r][v.c][v.dir]+sum[u.r][u.c][u.dir])%mod; } } } } int ans = INF; for(int i=0; i<4; i++) { if(d[r2][c2][i]!=-1) ans = min(ans,d[r2][c2][i]); } if(ans==INF) return -1; for(int i=0; i<4; i++) { if(ans==d[r2][c2][i]) { cnt = (cnt + sum[r2][c2][i])%mod; } } return cnt; } void _bfs(){ queue
q; memset(d,-1,sizeof(d)); Node u(r1,c1,dir); d[u.r][u.c][u.dir] = 0; q.push(u); vector
ans; cnt = 0; while(!q.empty()) { Node u = q.front(); q.pop(); if(u.r==r2&&u.c==c2) { if(ans.size()!=0) { if(ans[0]!=d[u.r][u.c][u.dir]) return ; else (cnt++)%mod; } else { cnt++; ans.push_back(d[u.r][u.c][u.dir]); } } for(int i=0; i<3; i++) { Node v = walk(u,i); if(a[v.r][v.c]=='.'&&inside(v.r,v.c)) { d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + 1; q.push(v); } } }} int main(){ int kase = 0; while(scanf("%d%d%d",&R,&C,&mod),R) { memset(d,-1,sizeof(d)); for(int i=0; i

 

转载于:https://www.cnblogs.com/TreeDream/p/6057399.html

你可能感兴趣的文章
gdb中信号的处理[转]
查看>>
学习Javascript闭包(Closure)
查看>>
LeetCode【709. 转换成小写字母】
查看>>
toString()和toLocaleString()有什么区别
查看>>
【mybatis】学习笔记之conf.xml与mapper.xml配置
查看>>
Python基础学习Day3 数据类型的转换、int、str、bool、字符串的常用方法、for循环...
查看>>
Controller比较两个对象discs、outlets中的元素是否相等。相同则相应的checkbox为checked...
查看>>
Android中在布局中写ViewPager无法渲染出来的问题
查看>>
简单shellcode编写
查看>>
centos7配置yum源
查看>>
winform textbox提示历史记录
查看>>
SSM整合(spring mybatis)图书
查看>>
Linux学习笔记--终端命令
查看>>
关于电脑桌面图标消失并且右键无法点击的情况
查看>>
JAVA窗口2
查看>>
【Alpha】第八次Scrum meeting
查看>>
学习进度条11
查看>>
剑指offer之【树的子结构】
查看>>
Http协议中常用字段总结(不定时完善中)
查看>>
大道至简——第二章读后感
查看>>