您的位置:首页 > 大数据 > 人工智能

山东省第一届ACM大学生程序设计竞赛 Fairy tale 大模拟

2016-05-03 15:39 447 查看


Fairy tale




Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^



题目描述

It is said that in a school’s underground, there is a huge treasure which can meet
any desire of the owner.
The Spy Union (SU) is very interest in this legend. After much investigation, SU finally get the answer and let the youngest
spy sneak into the school. That’s why Saya is now here.
Today, Saya found the entrance eventually.
She enters the entrance, and found her in a fairy-tale world.
“Welcome!” says a fairy, “I am Ivan. My responsibility is to protect the treasure, and give it to the one who have the
ability to own it.”
Then Ivan gives Saya three problems.
With your help, Saya finished the first and the second problem (Problem H and I). Here comes the third. If Saya can solve
this problem, the treasure belongs to her.
There is a big maze protecting the treasure. You can assume the maze as an N*N matrix
while each element in the matrix might be N (North), S (South), W (West) or E (East). At first, Saya is at the element (1, 1) – the north-west corner, and the treasure is at (N, N)
– the south-east corner.
The designer have enchant to this matrix, so that the treasure might move from time to time respecting the following rules:
Suppose the treasure is in an element which marked with E. The treasure might eastward move a cell after a unit time. It
is similar to S, W and N.
After a unit time, all the mark will change: E to W, W to S,
S to N, and N to E. In another word, suppose an element which marked with E at time 0. At time 1, it might change to W, change to S at time 2, change to N at time 3, change to E at time 4, and so on.
Saya doesn't know the initial status of the marks. She is affected by this rule, but she decides to do something more.
Ivan gave Saya a special prop which called Riki. With Riki’s help, she can get the position of the treasure.
In each unit time, Saya will do all of the following three things:
Firstly, she will check the treasure’s position with Riki.
Secondly, she will move follow the designer’s magic the same with the treasure.(If no element exists in the direction of
movement,the movement will be cancelled.)
Thirdly, Saya can either move to one direction (N, S, E, and W) a cell or stay there. Saya prefers to be closer with the
treasure; if there are many ways with the same geometrical distance, Saya prefers to stay there than move, prefer E than W, W than N, and N than S. Here we should use the position checked at the first step.
You are given the size of the matrix and all the marks of the elements at time 0.
Your task is to simulate Saya and the treasure’s movement, and then tell Saya the result.


输入

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N≤30),
which represents the size of the matrix.
Each of the next N lines contains a string whose length is N,
represents the elements of the matrix. The string only consists of N, E, W and S.
The last case is followed by a line containing one zero.


输出

For each case, print the case number (1, 2 …) and the result of Saya’s explore.
If Saya can get the treasure at step
x (x≤100)(that means at the begainning of time x, Saya and the treasure stay in
the same cell), print “Get the treasure! At step x.”. 
If after simulating x (x≤100)
steps, you found out that Saya can't get the treasure, print “Impossible. At step x.” 
If you have simulated 100 steps but don’t know whether Saya can get the treasure, print “Not sure.”
Your output format should imitate the sample output. Print a blank line after each
test case.


示例输入

5
EWSSE
NNENN
EENNE
SWSEW
NSNSW

0



示例输出

Case 1:
Get the treasure! At step 12.



提示

 Q&A
      Q: How can I know it’s impossible for Saya to get the treasure?
      A: Suppose at time 5, Saya at (1, 1) while the treasure at (2, 2); at time
13, Saya at (1, 1) while the treasure at (2, 2) again. Since both Saya and the treasure go to the same place and have the same direction again, it is a loop, and they will just repeats the moves forever. So at time 13, we can judge it is impossible.


来源

 2010年山东省第一届ACM大学生程序设计竞赛

示例程序

人在动图在动问能否找到宝藏

一开始以为是搜索后来发现是模拟

参考点击打开链接这篇博客

ACcode:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct P{
int px,py,tx,ty,sp;
}my[101];
int Map[31][31];
int dis[4][2]={0,1,0,-1,1,0,-1,0};
int n;
int t_x,t_y,flag,ttime,x,y;
bool better(int x,int y,int xx,int yy){
return (x-t_x)*(x-t_x)+(y-t_y)*(y-t_y) < (xx-t_x)*(xx-t_x)+(yy-t_y)*(yy-t_y);
}
bool can_step(int x,int y){
if(x<=0 || y<=0 || y>n || x>n) return 0;
return 1;
}
bool judge_re(){
int i;
for(i=0;i<ttime;++i)
if(my[i].px==x&&my[i].py==y&&my[i].tx==t_x&&my[i].ty==t_y&&my[i].sp==ttime%4){
flag=1;
return true;
}
my[i].px=x;my[i].py=y;my[i].tx=t_x;my[i].ty=t_y;my[i].sp==ttime%4;

return false;
}
bool step(){
int s1 = (Map[x][y]+ttime)%4;
int s2 = (Map[t_x][t_y]+ttime)%4;
if(judge_re()) return false;
if(x == t_x && y == t_y) return true;
int xx = x + dis[s1][0];
int yy = y + dis[s1][1];
if(can_step(xx,yy)) { x=xx,y=yy; }
xx=x,yy=y;
for(int i=0;i<4;i++){
int tx=x+dis[i][0];
int ty=y+dis[i][1];
if(can_step(tx,ty)&&better(tx,ty,xx,yy)){
xx=tx;
yy=ty;
}
}
x = xx, y = yy;
int txx = t_x + dis[s2][0];
int tyy = t_y + dis[s2][1];
if(can_step(txx,tyy)) { t_x=txx; t_y=tyy; }
++ttime;
return false;
}
int cnt=1;
void init(){
t_x=t_y=n;
ttime=flag=0;
x=y=1;
printf("Case %d:\n",cnt++);
}
void get_map(){
char c;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
cin>>c;
if(c=='E') Map[i][j]=0;
if(c=='W') Map[i][j]=1;
if(c=='S') Map[i][j]=2;
if(c=='N') Map[i][j]=3;
}
}
void doit(){
while(ttime < 100){
if(step()){
printf("Get the treasure! At step %d.\n\n", ttime);
return;
}
if(flag){
printf("Impossible. At step %d.\n\n", ttime);
return;
}
if(ttime == 99){
printf("Not sure.\n\n");
return;
}
}
}
int main(){
while(scanf("%d",&n)&&n){
get_map();
init();
doit();
}
return 0;
}

/**************************************
Problem id : SDUT OJ 2156
User name : acmer
Result : Accepted
Take Memory : 512K
Take Time : 0MS
Submit Time : 2016-05-03 15:37:59
**************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: