9161. VOYAGER
限制条件
时间限制: 1 秒, 内存限制: 256 兆
题目描述
The Voyager 1 space probe (not to be confused with the Intrepid-class starship) was launched a long
time ago, in 1977, and is currently on the verge of leaving our Solar System. As it travels further
through space, it has been programmed to leave a radio signal message in any star system it stumbles
upon, to mark the probe's path for as long as possible.
Let us assume that a star system can be represented by a rectangular grid with N rows and M
columns, dividing the space into N by M equal cells. Each cell can contain a single planet, black
hole, or be empty. The probe broadcasts the signal from a pre-determined empty cell, in one of the
four axis-aligned directions (U?up, R?right, D?down, L?left).
Upon being broadcast, the signal propagates in a straight line along the same row/column until it
reaches a planet, where it is deflected by 90 degrees in another direction. There are two kinds of
planets, which we will denote by / and \ The deflection rules are shown in the image below:
The signal permanently leaves the system upon either entring a cell containing a black hole, or
propagating outside the edges of the rectangular grid. It is also known that the signal needs one second
to propagate from the current cell to a neighbouring one.
Write a program to determine the direction in which the probe needs to broadcast the signal so that it
remains within the system for as long as possible, outputting the optimal direction as well as the
resulting longest time. If it is possible for the signal to remain in the system indefinitely, output the
message Voyager instead of the required time.
输入格式
The first line of input contains two positive integers, N (1 ≤ N ≤ 500) and M (1 ≤ M ≤ 500).
Each of the following N lines contains M characters from the set {“/”, “\”, “C”, “.”}, where “/” and
“\” represent the two kinds of planets, “C” represents a black hole, and “.” represents an empty cell.
The last line of input contains two positive integers, PR (1 ≤ PR ≤ N) and PC (1 ≤ PC ≤ M), the row
and column number, respectively, of the cell where the probe is situated.
输出格式
The first line of output must contain the required optimal broadcast direction (“U”, “R”, “D”, or “L”).
If the solution is not unique, select the first optimal one in the following priority order: first “U”, then
“R”, then “D”, and finally “L”.
The second line of output must contain the required longest time (or message).
样例输入
5 5 ../.\ ..... .C... ...C. \.../ 3 3
样例输出
U 17
题目来源
2013年每周一赛第10场/COCI 2013.1
这道题很郁闷,比较简单的模拟题。因为英文水平实在是太差了。居然不能理解题意叫我在是无限时输出什么。
一直都是wa在第五组数据。。。
还是要学好英语呀!!
#include<cstdio>
#include<cstring>
char map[505][505];
int flag[505][505][5];
int d[][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int k1[] = {3,2,1,0};
int k2[] = {1,0,3,2};
int main()
{
int row,col,i,j;
while(scanf("%d%d",&row,&col)!=EOF)
{
memset(map,'C',sizeof(map));
for(i=1;i<=row;i++)
{
scanf("%s",map[i]+1);
map[i][col+1] = 'C';
}
int si,sj;
scanf("%d%d",&si,&sj);
int dir;
int l,r,max=-1,v=0,dd;
for(i=0;i<4;i++)
{
memset(flag,0,sizeof(flag));
dir = i;
l = si;
r = sj;
flag[si][sj][i] = 1;
int p = 0;
int vivs = 0;
while(1)
{
if(map[l][r]=='\\')
{
dir = k1[dir];
}
if(map[l][r]=='/')
dir = k2[dir];
l = l+d[dir][0];
r = r+d[dir][1];
if(map[l][r]=='C')
break;
if(flag[l][r][dir]==1)
{
vivs = 1;
dd = i;
break;
}
flag[l][r][dir] = 1;
p++;
}
if(vivs == 1)
{
v = 1;
break;
}
else
{
v = 0;
if(max<p)
{
max = p;
dd = i;
}
}
}
if(v==1)
{
if(dd==0)
printf("U\nVoyager\n");
else if(dd==1)
printf("R\nVoyager\n");
else if(dd==2)
printf("D\nVoyager\n");
else
printf("L\nVoyager\n");
}
else
{
if(dd==0)
printf("U\n%d\n",max+1);
else if(dd==1)
printf("R\n%d\n",max+1);
else if(dd==2)
printf("D\n%d\n",max+1);
else
printf("L\n%d\n",max+1);
}
}
return 0;
}