很简单的模拟题目,代码有注释
Code
/**//*
ID: sdjllyh1
PROG: ttwo
LANG: JAVA
complete date: 2008/12/21
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class ttwo
{
private static int fFacing = 0;//人的面向,0表示北,1表示东,2表示南,3表示西
private static int cFacing = 0;//牛的面向,同上
private static char[][] forestMap = new char[10][10];//森林地图
private static int fX, fY, cX, cY;//人和牛的坐标
private static int minutes = 0;//模拟运行的时间
private static int answer;//输出的结果
private static boolean[][][][][][] appeared = new boolean[10][10][4][10][10][4];//记录某种状态是否出现过,前3个参数分别是人的x坐标、y坐标、面向,而后3个参数是牛的
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
//当人和牛没有遇见且此状态没有出现过
while ((!meet()) && (stateNotAppeared()))
{
//模拟进入下一分钟
enterNextMinute();
}
//如果他们见面了
if (meet())
{
//输出所用时间
answer = minutes;
}
else//否则没有见面
{
//输出0
answer = 0;
}
}
//进入下一分钟
private static void enterNextMinute()
{
if (canMove(fX, fY, fFacing))
{
fMove();
}
else
{
fRotate();
}
if (canMove(cX, cY, cFacing))
{
cMove();
}
else
{
cRotate();
}
minutes++;
}
//根据坐标与面向判断能否前进
private static boolean canMove(int x, int y, int facing)
{
boolean retCanMove = true;
switch (facing)
{
case 0:
if ((x == 0) || (forestMap[x - 1][y] == '*'))
{
retCanMove = false;
}
break;
case 1:
if ((y == 9) || (forestMap[x][y + 1] == '*'))
{
retCanMove = false;
}
break;
case 2:
if ((x == 9) || (forestMap[x + 1][y] == '*'))
{
retCanMove = false;
}
break;
case 3:
if ((y == 0) || (forestMap[x][y - 1] == '*'))
{
retCanMove = false;
}
break;
}
return retCanMove;
}
//人前进一步
private static void fMove()
{
switch (fFacing)
{
case 0:
fX--;
break;
case 1:
fY++;
break;
case 2:
fX++;
break;
case 3:
fY--;
break;
}
}
//牛前进一步
private static void cMove()
{
switch (cFacing)
{
case 0:
cX--;
break;
case 1:
cY++;
break;
case 2:
cX++;
break;
case 3:
cY--;
break;
}
}
//人旋转
private static void fRotate()
{
fFacing = (fFacing + 1) % 4;
}
//牛旋转
private static void cRotate()
{
cFacing = (cFacing + 1) % 4;
}
//判断是否相遇,相遇返回true
private static boolean meet()
{
if ((fX == cX) && (fY == cY))
{
return true;
}
else
{
return false;
}
}
//判断此状态是否出现过,没出现过返回true
private static boolean stateNotAppeared()
{
if (appeared[fX][fY][fFacing][cX][cY][cFacing])
{
return false;
}
else
{
appeared[fX][fY][fFacing][cX][cY][cFacing] = true;
return true;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("ttwo.in"));
for (int i = 0; i < 10; i++)
{
String str = f.readLine();
for (int j = 0; j < 10; j++)
{
forestMap[i][j] = str.charAt(j);
if (forestMap[i][j] == 'F')
{
fX = i;
fY = j;
}
if (forestMap[i][j] == 'C')
{
cX = i;
cY = j;
}
}
}
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ttwo.out")));
out.println(answer);
out.close();
}
}
/**//*
ID: sdjllyh1
PROG: ttwo
LANG: JAVA
complete date: 2008/12/21
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class ttwo
{
private static int fFacing = 0;//人的面向,0表示北,1表示东,2表示南,3表示西
private static int cFacing = 0;//牛的面向,同上
private static char[][] forestMap = new char[10][10];//森林地图
private static int fX, fY, cX, cY;//人和牛的坐标
private static int minutes = 0;//模拟运行的时间
private static int answer;//输出的结果
private static boolean[][][][][][] appeared = new boolean[10][10][4][10][10][4];//记录某种状态是否出现过,前3个参数分别是人的x坐标、y坐标、面向,而后3个参数是牛的
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
//当人和牛没有遇见且此状态没有出现过
while ((!meet()) && (stateNotAppeared()))
{
//模拟进入下一分钟
enterNextMinute();
}
//如果他们见面了
if (meet())
{
//输出所用时间
answer = minutes;
}
else//否则没有见面
{
//输出0
answer = 0;
}
}
//进入下一分钟
private static void enterNextMinute()
{
if (canMove(fX, fY, fFacing))
{
fMove();
}
else
{
fRotate();
}
if (canMove(cX, cY, cFacing))
{
cMove();
}
else
{
cRotate();
}
minutes++;
}
//根据坐标与面向判断能否前进
private static boolean canMove(int x, int y, int facing)
{
boolean retCanMove = true;
switch (facing)
{
case 0:
if ((x == 0) || (forestMap[x - 1][y] == '*'))
{
retCanMove = false;
}
break;
case 1:
if ((y == 9) || (forestMap[x][y + 1] == '*'))
{
retCanMove = false;
}
break;
case 2:
if ((x == 9) || (forestMap[x + 1][y] == '*'))
{
retCanMove = false;
}
break;
case 3:
if ((y == 0) || (forestMap[x][y - 1] == '*'))
{
retCanMove = false;
}
break;
}
return retCanMove;
}
//人前进一步
private static void fMove()
{
switch (fFacing)
{
case 0:
fX--;
break;
case 1:
fY++;
break;
case 2:
fX++;
break;
case 3:
fY--;
break;
}
}
//牛前进一步
private static void cMove()
{
switch (cFacing)
{
case 0:
cX--;
break;
case 1:
cY++;
break;
case 2:
cX++;
break;
case 3:
cY--;
break;
}
}
//人旋转
private static void fRotate()
{
fFacing = (fFacing + 1) % 4;
}
//牛旋转
private static void cRotate()
{
cFacing = (cFacing + 1) % 4;
}
//判断是否相遇,相遇返回true
private static boolean meet()
{
if ((fX == cX) && (fY == cY))
{
return true;
}
else
{
return false;
}
}
//判断此状态是否出现过,没出现过返回true
private static boolean stateNotAppeared()
{
if (appeared[fX][fY][fFacing][cX][cY][cFacing])
{
return false;
}
else
{
appeared[fX][fY][fFacing][cX][cY][cFacing] = true;
return true;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("ttwo.in"));
for (int i = 0; i < 10; i++)
{
String str = f.readLine();
for (int j = 0; j < 10; j++)
{
forestMap[i][j] = str.charAt(j);
if (forestMap[i][j] == 'F')
{
fX = i;
fY = j;
}
if (forestMap[i][j] == 'C')
{
cX = i;
cY = j;
}
}
}
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ttwo.out")));
out.println(answer);
out.close();
}
}