我自己在DEVC++和洛谷的在线IDE上跑第一组数据,答案是58; 但是提交上去,跑出来提示第一个数字是0...
#include <stdio.h>
#include <string.h>
int row[10], column[10], dir[10]; //下标:1表示农夫,2表示奶牛
char map[105][105]; //记录
int cr[15] = {0, -1, 0, 1, 0}, cc[15] = {0, 0, 1, 0, -1};
int ndir[15] = {0, 2, 3, 4, 1};
int mem[1000000] = {0}; //状态记忆器
void input();
int run();
void move(int who);
int status();
int istogether();
int main()
{
int min;
input();
min = run();
printf("%d", min);
return 0;
}
void move(int who)
{
int ni, nj;
ni = row[who] + cr[dir[who]];
nj = column[who] + cc[dir[who]];
if(map[ni][nj] == '*')
dir[who] = ndir[dir[who]];
else {
row[who] = ni;
column[who] = nj;
}
}
int status()
{
int far, cow;
far = (row[1]-1) + (column[1]-1) * 10 + (dir[1]-1) * 100;
cow = (row[2]-1) * 1000 + (column[2]-1) * 10000 + (dir[2]-1) * 100000;
return far + cow;
}
int istogether()
{
if(row[1] == row[2] && column[1] == column[2])
return 1;
else
return 0;
}
int run()
{
int sta, sum = 0;
while(!istogether()){
move(1);
move(2);
sum++;
sta = status();
if(mem[sta] == 1)
return 0;
else
mem[sta] = 1;
}
return sum;
}
void input()
{
char c;
for(int i = 1; i <= 10; i++){
for(int j = 1; j <= 10; j++){
c = getchar();
map[i][j] = c;
if(c == 'F'){
row[1] = i;
column[1] = j;
}
else if(c == 'C'){
row[2] = i;
column[2] = j;
}
}
getchar();
}
dir[1] = dir[2] = 1;
for(int i = 0; i <= 11; i++)
{
map[i][0] = '*';
map[i][11] = '*';
}
for(int j = 0; j <= 11; j++)
{
map[0][j] = '*';
map[11][j] = '*';
}
}