HDU 2414 Chessboard Dance (强行模拟)

题目链接:HDU 2414 Chessboard Dance

题意:给出一张图,>,<,^,v表示人面对的方向,字母相当于是箱子,箱子可以推出边界,人保证不会做出边界,下面输入指令,按照指令走,输出结束时图的状态;

强行模拟一遍,注意下面给出的案例。

>t.p.p.p
...a....
........
...bfg.y
...c...
...d....
...e....
........
move 3
turn right
move 3
turn left
move 3
turn left
move 3
#

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=20;
char mp[maxn][maxn];
queue<char> q;
int get_dir(int x,int y)
{
	if(mp[x][y]=='^')
		return 1;
	else if(mp[x][y]=='>')
		return 2;
	else if(mp[x][y]=='v')
		return 3;
	else if(mp[x][y]=='<')
		return 4;
}

void out()
{
	int i,j;
	//printf("---------------------\n");
	for(i=0; i<8; i++)
	{
		for(j=0; j<8; j++)
			printf("%c",mp[i][j]);
		printf("\n");
	}
	printf("\n");
}

int main()
{
	int i,j;
	int dir,x,y;
	char scr[20];
	while(scanf("%s",mp[0])!=EOF)
	{
		if(strcmp(mp[0],"--")==0)
			break;
		while(!q.empty())
			q.pop();
		for(i=1; i<8; i++)
			scanf("%s",mp[i]);
		for(i=0; i<8; i++)
		{
			for(j=0; j<8; j++)
			{
				if(mp[i][j]=='>' || mp[i][j]=='^' || mp[i][j]=='<' || mp[i][j]=='v')
				{
					x=i;
					y=j;
					dir=get_dir(x,y);
					break;
				}
			}
		}
		//out();
		while(1)
		{
			scanf("%s",scr);
			if(strcmp(scr,"#")==0)
				break;
			while(!q.empty())
				q.pop();
			if(strcmp(scr,"move")==0)
			{
				int ss;
				scanf("%d",&ss);
				if(dir==1)//向上
				{
					int temp;
					if(ss>=x)//!走出边界
					{
						temp=x;
						for(i=x;i>=0;i--)
							mp[i][y]='.';
						mp[x=0][y]='^';
					}
					else
					{
						mp[x][y]='.';
						int temp=x-1;
						int cont=ss;
						while(cont--)
						{
							while(mp[temp][y]!='.' && temp>=0)
							{
								q.push(mp[temp][y]);
								mp[temp][y]='.';
								temp--;
							}
							temp--;
						}
						temp=x-ss;
						mp[x=temp--][y]='^';
						for(;temp>=0 && !q.empty();temp--)
						{
							mp[temp][y]=q.front();
							q.pop();
						}
					}
					//out();
				}
				else if(dir==2)//向右
				{
					int temp;
					if(ss>=7-y)//!走出边界
					{
						temp=7-y;
						for(i=y;i<8;i++)
							mp[x][i]='.';
						mp[x][y=7]='>';
					}
					else
					{
						mp[x][y]='.';
						int temp=y+1;
						int cont=ss;
						while(cont--)
						{
							while(mp[x][temp]!='.' && temp<8)
							{
								q.push(mp[x][temp]);
								mp[x][temp]='.';
								temp++;
							}
							temp++;
						}
						temp=y+ss;
						mp[x][y=temp++]='>';
						for(;temp<8 && !q.empty();temp++)
						{
							mp[x][temp]=q.front();
							q.pop();
						}
					}
					//out();
				}
				else if(dir==3)//向下
				{
					int temp;
					if(ss>=7-x)//!走出边界
					{
						temp=7-x;
						for(i=x;i<8;i++)
							mp[i][y]='.';
						mp[x=7][y]='v';
					}
					else
					{
						mp[x][y]='.';
						int temp=x+1;
						int cont=ss;
						while(cont--)
						{
							while(mp[temp][y]!='.' && temp<8)
							{
								q.push(mp[temp][y]);
								mp[temp][y]='.';
								temp++;
							}
							temp++;
						}
						temp=x+ss;
						mp[x=temp++][y]='v';
						for(;temp<8 && !q.empty();temp++)
						{
							mp[temp][y]=q.front();
							q.pop();
						}
					}
					//out();
				}
				else if(dir==4)//向左
				{
					int temp;
					if(ss>=y)//!走出边界
					{
						temp=y;
						for(i=y;i>=0;i--)
							mp[x][i]='.';
						mp[x][y=0]='<';
					}
					else
					{
						mp[x][y]='.';
						int temp=y-1;
						int cont=ss;
						while(cont--)
						{
							while(mp[x][temp]!='.' && temp>=0)
							{
								q.push(mp[x][temp]);
								mp[x][temp]='.';
								temp--;
							}
							temp--;
						}
						temp=y-ss;
						mp[x][y=temp--]='<';
						for(;temp>=0 && !q.empty();temp--)
						{
							mp[x][temp]=q.front();
							q.pop();
						}
					}
					//out();
				}
			}
			else if(strcmp(scr,"turn")==0)
			{
				char cc[20];
				scanf("%s",cc);
				if(strcmp(cc,"left")==0)
				{
					if(dir==1) dir=4,mp[x][y]='<';
					else if(dir==2) dir=1,mp[x][y]='^';
					else if(dir==3) dir=2,mp[x][y]='>';
					else if(dir==4) dir=3,mp[x][y]='v';
				}
				else if(strcmp(cc,"right")==0)
				{
					if(dir==1) dir=2,mp[x][y]='>';
					else if(dir==2) dir=3,mp[x][y]='v';
					else if(dir==3) dir=4,mp[x][y]='<';
					else if(dir==4) dir=1,mp[x][y]='^';
				}
				else if(strcmp(cc,"back")==0)
				{
					if(dir==1) dir=3,mp[x][y]='v';
					else if(dir==2) dir=4,mp[x][y]='<';
					else if(dir==3) dir=1,mp[x][y]='^';
					else if(dir==4) dir=2,mp[x][y]='>';
				}
				//out();
			}
		}
		out();
	}
	return 0;
}
/*
.....c..
.p..A..t
D..>T.Pr
....aP.P
p.d.C...
.....p.R
........
........
move 5

.....c..
.p..A..t
D..>T.Pr
....aP.P
p.d.C...
.....p.R
........
........
move 1
turn left
move 99
turn back
move 6
#
.....c..
.p.....t
D....TPr
.....P.P
p.d.C...
.....p.R
....v...
....a...

>t.p.p.p
...a....
........
...bfg.y
...c...
...d....
...e....
........
move 3
turn right
move 3
turn left
move 3
turn left
move 3
#

.>ta.cb.
........
........
........
........
........
........
........
move 3
#
*/
时间: 2024-10-10 22:59:40

HDU 2414 Chessboard Dance (强行模拟)的相关文章

POJ 3344 &amp; HDU 2414 Chessboard Dance(模拟)

题目链接: PKU:http://poj.org/problem?id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Description Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living r

POJ 3344 &amp;amp; HDU 2414 Chessboard Dance(模拟)

题目链接: PKU:http://poj.org/problem? id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Description Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living

HDU 2414 Chessboard Dance(模拟题,仅此纪念我的堕落)

题目 模拟题也各种wa,我最近真的堕落了,,,,,智商越来越为负数了!!!!!!!! #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char mp[10][10]; int d=-1;//0shang,1xia,2zuo,3you int x,y;//weizhi int weizhi(int i,int j) { if(mp[i][j]=='<'){x=

【HDOJ】2414 Chessboard Dance

简单DFS. 1 /* 2414 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 const int n = 8; 7 char map[10][10]; 8 int x, y, d; 9 char dirs[5] = "^v<>"; 10 int dir[4][2] = { 11 -1,0, 1,0, 0,-1, 0,1 12 }; 13 int

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

hdu 4891 The Great Pan(模拟)

题目链接:hdu 4891 The Great Pan 题目大意:给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn+1), si表示连续的空格数. 2.{}中间,即 | 的个数+1. 解题思路:模拟. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1<<22

HDU 4930 Fighting the Landlords 模拟

_(:зゝ∠)_ 4带2居然不是炸弹,, #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #include <cstring> #include <climits> #include <vector> #include<iostream> using namespace std; #define N 18 #

hdu 1316 How Many Fibs? (模拟高精度)

题目大意: 问[s,e]之间有多少个 斐波那契数. 思路分析: 直接模拟高精度字符串的加法和大小的比较. 注意wa点再 s 可以从 0 开始 那么要在判断输入结束的时候注意一下. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct node { char str[111]; int len; void

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊