ZOJ 1056 The Worm Turns
题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活。
解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标。每读入一条指令,判断其是否越界,是否咬到了自己。若都没有,把改点压入队列,front元素弹出。
参考代码:
#include<iostream>
#include<queue>
#include<string>
using namespace std; struct Point{
int r;
int c;
}; queue<Point> worm; bool isItself(queue<Point>,Point); int main(){
int i,n;
char str[102];
Point temp;
char x;
while(cin>>n&&n!=0){
while(!worm.empty())
worm.pop();
for(i=11;i<=30;i++){
temp.c=i;
temp.r=25;
worm.push(temp);
}
cin>>str;
i=1;
while(i<=n){
x=str[i-1];
temp=worm.back();
if(x=='W'){
temp.c-=1;
if((temp.c)<1){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop(); }else{
if(x=='E'){
temp.c+=1;
if((temp.c)>50){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}else{
if(x=='S'){
temp.r+=1;
if((temp.r)>50){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}else{
if(x=='N'){
temp.r-=1;
if((temp.r)<1){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}
}
}
} if(i==n)
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
i++;
}
} return 0;
} bool isItself(queue<Point> worm,Point p){
int k;
queue<Point> w;
w=worm;
w.pop();
for(k=1;k<19;k++){
if(w.front().r==p.r&&w.front().c==p.c)
return true;
w.pop();
}
return false;
}
ZOJ 1056 The Worm Turns的更多相关文章
- TOJ 1191. The Worm Turns
191. The Worm Turns Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 5465 Accepted Run ...
- The Worm Turns
The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- TJU ACM-ICPC Online Judge—1191 The Worm Turns
B - The Worm Turns Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- 【HDOJ】2782 The Worm Turns
DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...
- ZOJ 1494 Climbing Worm 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- PowerMock使用遇到的问题——2
如果在测一个类的某一个方法时,这个方法还调用了此类的其他方法,那么如何指定其他方法的返回值呢? Partial mock local private method or public method i ...
- for循环语句示例应用
age = 22 #优化前 ''' for i in range(10): guess_num = int(input('input your guess num:')) if guess_num = ...
- How to setup SVN?
2014-01-08 11:43:50 如何简单设置SVN(前提是SVN已经安装) 1. 创建一个目录: mkdir -p ~/svn/2.1.J.1.1 2. 进入新创建的目录: cd svn/2. ...
- 读写其他应用程序的SharedPreference
2013-12-28 18:03:40 要读写其他应用的SharedPreference,前提是创建该SharedPreference的程序指定相应的可读或可写的权限, 如下: private voi ...
- debug实战:进程Hang+High CPU
最近几周都在解决程序不稳定的问题,具体表现为程序(多进程)时不时的Hang住,同时伴随某个进程的High CPU.跟踪下来,基本都是各种死锁引起的.这里选取一个典型的场景进行分析. 1.抓dump分析 ...
- 【STL】-pair的用法
初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...
- 数组的foreach方法和jQuery中的each方法
/* * 数组的forEach方法: * 1.返回给回调的参数先是值,然后是下标 * 2.回调函数执行时内部的this指向window * */ /*var arr = [1,2,3,4,5]; ar ...
- JavaScript基础--超级玛丽(七)(上下左右控制)
相信大家都玩过超级玛丽,下面实现控制玛丽的上.下.左.右等基本功能,本篇只是在练习JavaScript的用法 1.创建一个HTML页面 <!doctype html> <html l ...
- Event Handling on Mac
Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...
- 二、XML约束
XML约束有dtd约束和Schema约束两种 dtd约束:可以在xml内部写dtd约束也可以在xml中引用外部dtd文件 book.dtd<!ELEMENT 书架 (书+)> < ...