原题链接

题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活。

解法:这条蛇一共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的更多相关文章

  1. TOJ 1191. The Worm Turns

    191.   The Worm Turns Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 5465   Accepted Run ...

  2. The Worm Turns

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. 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 ...

  4. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  5. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  6. ZOJ 1494 Climbing Worm 数学水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...

  7. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  8. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  9. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

随机推荐

  1. POJ 2262 Goldbach's Conjecture 数学常识 难度:0

    题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分 ...

  2. tab模块

    #!/usr/bin/env python # python startup file import sys import readline import rlcompleter import ate ...

  3. [vijos P1626] 爱在心中

    做完Victoria的舞会3,挑了vijos里强连通分量里面难度值最低的题目,也就是这道.先把第一小问做了,纯Tarjan,只是我学的时候的标程是用邻接表的,这题数据小于是用了邻接矩阵,两者之间的切换 ...

  4. QuartZ的线程锁

                                                                                                ;        ...

  5. C语言中数组的几种输入

  6. Add project to working sets

    最近换了个电脑,重新搭建了开发环境,但是在新建项目的过程中发现有Add project to working sets这一个选项,一开始也不明白是什么意思,百度了一下,不少网友说是把项目存到物理空间, ...

  7. SharePoint 2010 BCS - 概述

    博客地址 http://blog.csdn.net/foxdave SharePoint 2010首次引入了BCS的概念 - Business Connectivity Service,即业务连接服务 ...

  8. Matlab单一变量曲线拟合-cftool

    2.启动曲线拟合工具箱>cftool 3.进入曲线拟合工具箱界面“Curve Fitting tool”(1)点击“Data”按钮,弹出“Data”窗口:(2)利用X data和Y data的下 ...

  9. C++数据结构之Queue(队列)

    Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...

  10. (spring-第16回【AOP基础篇】)基本概念

    AOP(Aspect Oriented Programing),面向切面方程.介绍具体定义前,先看一个例子: package com.baobaotao.concept; public class F ...