POJ-1573 Robot Motion模拟
题目链接:
https://vjudge.net/problem/POJ-1573
题目大意:
有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ,走到某个区域的时候只能按照该区域指定的方向进行下一步,问你机器人能否走出该片区域,若不能,输入开始绕圈的步数和圈的大小。
思路:
一开始一直WA,后来发现是因为vis数组的问题,这里设置的vis数组表示到该点走的步数,最开始是0步,这和初始化是0步是一样的,会导致如果再次走到这个点,就会判断成该点没有走过,后来初始化成-1,A了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + ;
const int INF = << ;
int dir[][] = {,,,,-,,,-};
int T, n, m, x;
map<char, int>M;
int Map[][];
int vis[][];
void dfs(int x, int y, int d)
{
//cout<<x<<" "<<y<<" "<<d<<endl;
if(x <= || x > n || y <= || y > m)
{
cout<<d<<" step(s) to exit"<<endl;
return;
}
if(vis[x][y] != -)
{
cout<<vis[x][y]<<" step(s) before a loop of "<<d - vis[x][y]<<" step(s)"<<endl;
return;
}
int cn = Map[x][y];
vis[x][y] = d;
dfs(x + dir[cn][], y + dir[cn][], d + );
}
int main()
{
M['S'] = ;
M['E'] = ;
M['N'] = ;
M['W'] = ; while(cin >> n >> m >> x)
{
if(!n && !m && !x)break;
memset(vis, -, sizeof(vis));
//这里不能设置成0,应该设置成-1,因为设置成0的话,第一步出发设置的也是0,这样就会把第一步标记成未走过的点了
memset(Map, , sizeof(Map));
char c;
for(int i = ; i <= n; i++)
{
for(int j = ;j <= m; j++)
{
cin >> c;
Map[i][j] = M[c];
}
}
dfs(, x, );
}
return ;
}
POJ-1573 Robot Motion模拟的更多相关文章
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- poj 1573 Robot Motion_模拟
又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- PKU 1573 Robot Motion(简单模拟)
原题大意:原题链接 给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去) 此题虽然属于水题,但是完全独立完成而且直接1A还是很 ...
随机推荐
- Android Service 基础
启动方式 startService(Intent) 这种方式启动的Service可以在后台无限期的运行,与启动它的组件没有关系. bindService 绑定Service.它提供了一种类似C/S结构 ...
- Android_scaleType属性
这里我们重点理解ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType).android:scaleType ...
- Matlab绘图基础——给图像配文字说明(text对象)
text对象 (1)text(x坐标,y坐标,'string')在图形中指定位置(x,y)显示字符串string.(2)Editing有效值为on/off,off时,用户在执行GUI操作时无法直接 ...
- KVM之十一:调整cpu和内存
1.virsh edit snale (更改前要将snale shutdown) 找到"memory"和"vcpu"标签,将 <memory unit=' ...
- 控制反转( IoC)和依赖注入(DI)
控制反转( IoC)和依赖注入(DI) tags: 容器 依赖注入 IOC DI 控制反转 引言:如果你看过一些框架的源码或者手册,像是laravel或者tp5之类的,应该会提到容器,依赖注入,控制反 ...
- python全栈学习--day4
列表 说明:列表是python中的基础数据类型之一,它是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: 1 li = ['alex',123,Ture,(1,2,3,'wu ...
- 【R语言系列】作图入门示例一
假设有如下数据,我们使用plot函数作图 月龄 体重 月龄 体重 1 4.4 9 7.3 3 5.3 3 6.0 5 7.2 9 10.4 2 5.2 12 10.2 11 8.5 3 6.1 R语 ...
- JavaScript(第三十三天)【总结:封装基础前端框架】
源码地址:https://github.com/whisper540/Base
- %f使用时的注意事项
1不是所有定义都用int,使用浮点函数需要把int改成float才能正常工作 2保留一位小数时要打入%0.1f,保留两位小数时要打入%0.2f,而不是%0.01f
- Beta第四天
听说