题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1、向前走;2、左转90度;3、右转90度。现给定起点和终点,问到达终点最短路的条数。

思路:一般的题目只是求最短路的长度,但本题还要求出相应的条数。比赛时只记录最少的步数,却没有记录以最少步数到达该点的的条数,让他们一直入队.......铁定tle.......

只要记录好到达该点最少步数的条数,减少了很多重复入队..........

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#define MAX 1111
#define INF 0x7FFFFFFF
using namespace std; int n,m,mod;
struct node {
int x,y,dir;
} st,end; queue <node> q;
char map[MAX][MAX];
int num[MAX][MAX][4]; //步数
int cnt[MAX][MAX][4]; //多少种路径以该方向到达该点
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1}; void init() {
while(!q.empty()) q.pop();
memset(num,-1,sizeof(num));
memset(cnt,0,sizeof(cnt));
} int getdir(char c) {
if(c == 'N') return 0;
if(c == 'E') return 1;
if(c == 'S') return 2;
if(c == 'W') return 3;
} bool ok(int x,int y) {
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] == '.') return true;
return false;
} void bfs() {
num[st.x][st.y][st.dir] = 0;
cnt[st.x][st.y][st.dir] = 1;
q.push(st);
while(! q.empty()) {
node t = q.front();
q.pop();
node tt;
//cout << t.x << ' ' << t.y << ' ' << num[t.x][t.y][t.dir] << endl;
for(int i=0; i<4; i++) { //转方向
if(abs(t.dir - i) == 1 || abs(t.dir - i) == 3) {
tt = t;
if(num[t.x][t.y][i] == -1) {
num[t.x][t.y][i] = num[t.x][t.y][t.dir] + 1;
cnt[t.x][t.y][i] = cnt[t.x][t.y][t.dir];
tt.dir = i;
q.push(tt);
} else if(num[t.x][t.y][i] == num[t.x][t.y][t.dir] + 1) {
cnt[t.x][t.y][i] = (cnt[t.x][t.y][i] + cnt[t.x][t.y][t.dir]) % mod;
}
}
}
tt = t;
tt.x += dx[tt.dir];
tt.y += dy[tt.dir];
if(ok(tt.x,tt.y)) {
if(num[tt.x][tt.y][tt.dir] == -1) { //移动
num[tt.x][tt.y][tt.dir] = num[t.x][t.y][t.dir] + 1;
cnt[tt.x][tt.y][tt.dir] = cnt[t.x][t.y][t.dir];
q.push(tt);
} else if(num[tt.x][tt.y][tt.dir] == num[t.x][t.y][t.dir] + 1) {
cnt[tt.x][tt.y][tt.dir] = (cnt[tt.x][tt.y][tt.dir] + cnt[t.x][t.y][t.dir]) % mod;
}
}
}
} void solve() {
int ans = INF;
for(int i=0; i<4; i++) {
if(num[end.x][end.y][i] != -1) {
ans = min(ans,num[end.x][end.y][i]);
}
}
if(ans == INF) {
printf("%d -1\n",mod);
return ;
}
int sum = 0;
for(int i=0; i<4; i++) {
if(num[end.x][end.y][i] == ans) sum = (sum + cnt[end.x][end.y][i]) % mod;
}
printf("%d %d\n",mod,sum);
}
int main() {
char c;
int ca = 1;
while(scanf("%d%d%d",&n,&m,&mod) != EOF) {
if(mod == 0) break;
init();
for(int i=0; i<n; i++) scanf("%s",map[i]);
scanf("%d%d%d%d",&st.x,&st.y,&end.x,&end.y);
scanf(" %c",&c);
st.dir = getdir(c);
bfs();
printf("Case %d: ",ca++);
solve();
}
return 0;
}

HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)的更多相关文章

  1. HDU 4960 Another OCD Patient(记忆化搜索)

    HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...

  2. 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...

  3. HDU 4628 Pieces(状态压缩+记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索  ...

  4. HDU 4597 Play Game (DP,记忆化搜索,博弈)

    题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...

  5. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  6. HDU 1331 Function Run Fun(记忆化搜索)

    Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...

  7. HDU - 5001 Walk(概率dp+记忆化搜索)

    Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...

  8. BNU 25593 Prime Time 记忆化dp

    题目链接:点击打开链接 题意: 一个游戏由3个人轮流玩 每局游戏由当中一名玩家选择一个数字作为開始 目的:获得最小的得分 对于当前玩家 O .面对 u 这个数字 则他的操作有: 1. 计分 u +1 ...

  9. HDU 3652 B-number(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP G - B-number 题意 求1-n的范围里含有13且能被13整除的数字的个数. 思路 首先,了解这样一个式子:a%m == ((b%m)* ...

随机推荐

  1. zoj 2277 The Gate to Freedom

    N^N = X --->    Nlog10(N) = log10( X ) ---->    X的最高位为 Nlog10(N) 小数部分的第一个非0位 #include<stdio ...

  2. PHP设计模式——策略模式

    概述 策略模式属于对象的行为模式.其用意是针对一组算法,将每个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化 UML图 策略模式中主 ...

  3. Fix Windows 7 Msvcp71.dll And Msvcr71.dll Missing Error

    Fix Windows 7 Msvcp71.dll And Msvcr71.dll Missing Error Fix Msvcp71.dll And Msvcr71.dll Missing Erro ...

  4. poj 3767 I Wanna Go Home

    题意:n个点(从1-n编号) m条边 下面m行 u v dis 表示双向边u v的距离 n个点表示 每个点被势力1或2占据 这里保证1 城市由势力1占据,2城市由势力2占据 思路: 求2遍spfa() ...

  5. 【JSP】JSP与oracle数据库交互案例

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  6. db2常用命令(详解)大全

    近一年来在项目开发中使用到了IBM的DB2 9.1的数据库产品,跟Oracle相比一些命令有很大的区别,而它最大的功能是支持      xml存储.检索机制,通过XPath进行解析操作,使开发人员免于 ...

  7. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  8. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  9. 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168

    目前比较流行的是使用SWFUpload控件,这个控件的详细介绍可以参见官网http://demo.swfupload.org/v220/index.htm 在使用这个控件批量上传文件时发现中文文件名都 ...

  10. Linux下mpi环境配置与执行步骤(Ubuntu为例)

    转载注明出处: http://blog.csdn.net/bendanban/article/details/9136755 以两台计算机为例,将这两台计算机应用于MPI运行环境. 第一步:在两台机器 ...