题意:给出一个矩形,N,E,S,W分别代表进行移动的方向,如果走出矩形网格则输出经过的网格数,如果在矩形网格内循环,则输出没进入循环之前所走过的网格数和循环所经过的网格数;

思路:创建两个数组,一个字符数组存每个网格中所要进行的操作,另一个整型数组代表状态,0代表没走过,1代表走过了,然后模拟;

   再提醒一点,建立的字符和整型数组要和题目中建里的一模一样,因为这个我又WA了一发、

   

#include<iostream>
#include<cstring>
using namespace std;
const int qq=15;
int gid[qq][qq];char s[qq][qq];
int main()
{
int n,m,k;
while(cin >> n >> m >> k)
{
cin.get();
if(n==0&&m==0&&k==0) break;
memset(gid,0,sizeof(gid));
for(int i=0;i<=m+1;++i){
gid[0][i]=1;gid[n+1][i]=1;  //外围标记
}
for(int i=0;i<=n+1;++i){        //外围标记
gid[i][0]=1;gid[i][m+1]=1;
}
for(int j,i=1;i<=n;++i){
for(j=1;j<=m;++j)
s[i][j]=cin.get();
cin.get();
}
int x,y;x=k;y=1;int tot=0;
while(!gid[y][x]){              
while(!gid[y][x]&&s[y][x]=='N'){
gid[y][x]=1;y-=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='E'){
gid[y][x]=1;x+=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='S'){
gid[y][x]=1;y+=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='W'){
gid[y][x]=1;x-=1;++tot;
}
}
if(x<1||y<1||x>m||y>n) cout << tot << " step(s) to exit\n";  
else{
int count=0;
while(gid[y][x]){                //计算循环所经过的网格数  此时1,0的意义互换
while(gid[y][x]&&s[y][x]=='N'){
gid[y][x]=0;y-=1;++count;
}
while(gid[y][x]&&s[y][x]=='E'){
gid[y][x]=0;x+=1;++count;
}
while(gid[y][x]&&s[y][x]=='S'){
gid[y][x]=0;y+=1;++count;
}
while(gid[y][x]&&s[y][x]=='W'){
gid[y][x]=0;x-=1;++count;
}
}
cout << tot-count << " step(s) before a loop of " << count << " step(s)\n";
}
}
}

  

poj1573的更多相关文章

  1. POJ-1573 Robot Motion模拟

    题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...

  2. poj1573 Robot Motion(DFS)

    题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...

  3. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  4. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

  5. poj1573 模拟

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11270   Accepted: 5487 Des ...

  6. POJ1573——Robot Motion

    Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...

  7. POJ1573 Robot Motion(模拟)

    题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...

  8. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  9. poj1573模拟

    Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...

  10. 快速切题 poj1573

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10708   Accepted: 5192 Des ...

随机推荐

  1. (一)学习SpringBoot介绍

    为什么去要用到SpringBoot 特点: 创建独立的spring应用 嵌入tomcat ,jetty不需要部署 提供starters poms简化maven配置 尽可能自动配置spring'应用 提 ...

  2. Springboot 创建的maven获取resource资源下的文件的两种方式

    Springboot 创建的maven项目 打包后获取resource下的资源文件的两种方式: 资源目录: resources/config/wordFileXml/wordFileRecord.xm ...

  3. 7 种 Javascript 常用设计模式学习笔记

    7 种 Javascript 常用设计模式学习笔记 由于 JS 或者前端的场景限制,并不是 23 种设计模式都常用. 有的是没有使用场景,有的模式使用场景非常少,所以只是列举 7 个常见的模式 本文的 ...

  4. laravel 图片

    /** * 缩略图上传 */ public static function addPic() { $inputData = request()->all(); $rules = [ 'main_ ...

  5. Data Lake Analytics中OSS LOCATION的使用说明

    前言 Data Lake Analytic(后文简称 DLA)可以帮助用户通过标准的SQL语句直接对存储在OSS.TableStore上的数据进行查询分析. 在查询前,用户需要根据数据文件的格式和内容 ...

  6. 洛谷1014 Cantor表

      水题.随便搞搞就过了. //Serene #include<algorithm> #include<iostream> #include<cstring> #i ...

  7. bzoj1911 特别行动队

    Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9       斜率优化 推式子 #include< ...

  8. KiCad 不可以画线宽小于 0.2mm 的走线?

    KiCad 不可以画线宽小于 0.2mm 的走线? 有小伙伴在 QQ 群里反馈,KiCad 设置线宽规则时出现错误. 于是判断 KiCad 不可以画 BGA PCB,很显然我认为这是不可能的事情. 作 ...

  9. QT_OPENGL-------- 1. WINDOW

    opengl学习第一步,首先来实现一个显示窗口. 1.首先要下载配置glfw,我在前面的文章中也提到过,具体作用可以另行百度.配置出现无法引用可参考ubuntu 使用glfw.h 出现函数无法调用. ...

  10. UVa 10285【搜索】

    UVa 10285 哇,竟然没超时!看网上有人说是记忆化搜索,其实不太懂是啥...感觉我写的就是毫无优化的dfs暴力....... 建立一个坐标方向结构体数组,每个节点dfs()往下搜就好了. #in ...