hdu~4166~Robot Navigation

news/2025/3/14 5:28:44/

此题用的广搜加记忆化,一开始没考虑方向,测试数据都过不了,在队友的提醒下才发现,改了之后广搜过了,深搜却错了,调了无数个小错误之后,终于过了测试数据,汗啊!!!交上去却Wrong了,但是就郁闷啦,逻辑明明没错了,最后还是在队友的帮助下才发现没考虑终点不可达的情况。功夫不负有心人,断断续续等了一个星期,终于AC了。

 

ACcode:

 

#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
const int size=1010;
const int MAX=1000000001;
struct Point 
{
int x,y,step,dir;
}end,start;
void bfs();
int dfs(Point cur);
bool ismap(Point a);
int judgestr(char c);
void insert(Point next);
int stepnum(Point next,Point cur);
int n,m,mo;
queue<Point> q; 
char str[size][size],c;
int num[size][size][4],dp[size][size][4];
Point dir[4]={{1,0},{0,-1},{-1,0},{0,1}};
int judgestr(char c)
{
if (c=='N') return 0;
else if (c=='E') return 1;
else if (c=='S') return 2;
else return 3;
}
bool ismap(Point a)
{
if (a.x<0||a.x>=n||a.y<0||a.y>=m||str[a.x][a.y]=='*') return false;
return true;
}
void insert(Point next)
{
if (ismap(next)&&next.step<num[next.x][next.y][next.dir])
{
num[next.x][next.y][next.dir]=next.step;
q.push(next);
}
return ;
}
void bfs()
{
int i,j,k;
Point t,next;
t.x=end.x,t.y=end.y,t.step=0;
while (!q.empty()) q.pop();
for (i=0;i<4;i++) 
{
t.dir=i;
q.push(t);    
}    
while (!q.empty())
{
t=q.front();
q.pop();  
next.x=t.x+dir[t.dir].x;
next.y=t.y+dir[t.dir].y;
next.dir=t.dir;
if (next.step!=MAX)
{
next.step=t.step+1;
insert(next);
}
next.x=t.x;
next.y=t.y;
next.dir=(t.dir+1)%4;
if (next.step!=MAX)
{
next.step=t.step+1;
insert(next);
}
next.dir=(t.dir+3)%4;
if (next.step!=MAX)
{
next.step=t.step+1;
insert(next);
}
}
return ;
}
int stepnum(Point next,Point cur)
{
if (ismap(next)&&num[next.x][next.y][next.dir]==num[cur.x][cur.y][cur.dir]-1)
return dfs(next);  
else return 0;
}
int dfs(Point cur)
{
if (dp[cur.x][cur.y][cur.dir]) return dp[cur.x][cur.y][cur.dir]; 
int i,ans=0;
Point next;
next.x=cur.x+dir[(cur.dir+2)%4].x;
next.y=cur.y+dir[(cur.dir+2)%4].y;
next.dir=cur.dir;
ans=(ans+stepnum(next,cur))%mo;
next.x=cur.x;
next.y=cur.y;
next.dir=(cur.dir+1)%4;
ans=(ans+stepnum(next,cur))%mo;
next.dir=(cur.dir+3)%4;
ans=(ans+stepnum(next,cur))%mo;
dp[cur.x][cur.y][cur.dir]=ans;
return ans;
}
int main()
{
int i,j,k,cas=0;
while (scanf("%d %d %d",&n,&m,&mo)&&n&&m&&mo)
{
for (i=0;i<n;i++) scanf("%s",str[i]);
scanf("%d %d %d %d %c",&start.x,&start.y,&end.x,&end.y,&c);
start.dir=judgestr(c);
for (i=0;i<n;i++) for (j=0;j<m;j++) 
for (k=0;k<4;k++) num[i][j][k]=MAX,dp[i][j][k]=0;
for (i=0,k=-1;i<4;i++) num[end.x][end.y][i]=0,dp[end.x][end.y][i]=1;
bfs();
if (num[start.x][start.y][start.dir]!=MAX&&str[end.x][end.y]!='*') k=dfs(start);
printf("Case %d: %d %d\n",++cas,mo,k);   
}
return 0;   
}


http://www.ppmy.cn/news/243422.html

相关文章

bzoj4166 月宫的符卡序列(manacher+链状双hash)

分析&#xff1a; 网上的题解少之又少&#xff0c;而且都是dalao码风&#xff0c;所以只能自力更生了 仔细分析一下&#xff0c;这道题就是求解本质不同的回文串以及出现位置 由于字符串比较长&#xff0c;我们需要用manacher算法 在manacher算法中&#xff0c;只有发生了扩…

【洛谷 P4166】 [SCOI2007]最大土地面积(凸包,旋转卡壳)

题目链接 又调了我两个多小时巨亏 直接\(O(n^4)\)枚举4个点显然不行。 数据范围提示我们需要一个\(O(n^2)\)的算法。 于是\(O(n^2)\)枚举对角线&#xff0c;然后在这两个点两边各找一个点使其和对角线构成的三角形面积最大&#xff0c;也就是叉积的绝对值最大。显然具有单调性&…

HDU 4166 BNU 32715 Robot Navigation (记忆化bfs)

题意&#xff1a;给一个二维地图&#xff0c;每个点为障碍或者空地&#xff0c;有一个机器人有三种操作&#xff1a;1、向前走&#xff1b;2、左转90度&#xff1b;3、右转90度。现给定起点和终点&#xff0c;问到达终点最短路的条数。 思路&#xff1a;一般的题目只是求最短路…

P4166 [SCOI2007]最大土地面积

传送门 首先&#xff0c;四边形的四个点肯定都在凸包上&#xff08;别问我为什么我也不知道&#xff0c;感性理解一下好了&#xff09; 那么我们可以求出凸包之后\(O(n^4)\)暴力枚举&#xff0c;据说在随机数据下凸包上的点只有\(O(logn)\)个可过 然而出题人大大的没有良心&…

HDU 4166 Robot Navigation

题意&#xff1a; 一个机器人走迷宫 每一秒要么转向要么前进 问 最少时间的情况下有几种方案 思路&#xff1a; 记忆化搜索即可 简单bfs 代码&#xff1a; #include<cstdio> #include<iostream> #include<cstring> #include<string> #include&…

HDU4166【BFS】

题意&#xff1a; 给你一幅图&#xff0c;给你起点和终点&#xff0c;再给你机器人所面对的方向&#xff0c;机器人可以左转90&#xff0c;右转90&#xff0c;向前进一格&#xff0c;每种操作都是1秒&#xff0c;,求起点和终点最少花费下的路径条数&#xff0c;方案数&#xf…

[回文自动机 Manacher] BZOJ4166: 月宫的符卡序列

hash被卡… 本来以为是回文自动机裸题 发现fail树上一条链的节点表示的回文子串的中点是不一样的… 不过回文树上的链是一样的 那么用建出回文树&#xff08;我用回文自动机建的&#xff0c;manacher建不知道为什么WA了&#xff09;&#xff0c;然后找到以每个点为中点的最…

【BZOJ4166】月宫的符卡序列 Manacher+hash

【BZOJ4166】月宫的符卡序列 题解&#xff1a;题倒不难&#xff0c;就是有点恶心。 首先学习回文串的时候一定学到了这样一个结论&#xff1a;一个长度为n的串的本质不同的回文子串数量不超过n个。 那么我们就可以试图将所有回文串的价值都计算出来&#xff0c;这就需要我们先计…