![java:P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two,第1张 java:P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two,第1张](/aiimages/java%EF%BC%9AP1518+%5BUSACO2.4%5D%E4%B8%A4%E5%8F%AA%E5%A1%94%E5%A7%86%E6%B2%83%E6%96%AF%E7%89%9B+The+Tamworth+Two.png)
洛谷题目:P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two
题目如下: 思路:我的代码简单的模拟,不过像这种上下左右的移动方式倒是可以留意下,我是用%的方式来改变的。
import java.io.*;
public class Main {
static char[][] chars = new char[10][10];
static int rx = 0,ry = 0; // 记录人的位置
static int nx = 0,ny = 0; // 记录牛的位置
static int lt = 0; // 转向时牛的朝向记录
static int rlt = 0; // 转向时人的朝向记录
static int[] ints = {-1,0,0,1,1,0,0,-1}; // 依次为向北、东、南、西走一步
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
char ct; // 在循环中定义变量好像会拖慢些速度,所以我尽量养成不在循环中定义变量
for(int i = 0;i<10;i++)
{
String strl = ins.readLine(); // 读取一行
for(int i1 = 0;i1<10;i1++)
{
ct = strl.charAt(i1); // 根据String的charAt()来建立地图
chars[i][i1] = ct;
if(ct == 'C') // 如果该点是牛,则记录牛的位置
{
nx = i;
ny = i1;
}
else if (ct == 'F') // 如果该点是人,则记录人的位置
{
rx = i;
ry = i1;
}
}
}
int count = 0; // 计数用的
while(rx!=nx||ry!=ny) // 当rx = nx && ry = ny时说明此时找到了牛,所以跳出循环
{
nYD(); // 牛移动or转向(这是函数中的事了)
rYD(); // 人移动or转向(这是函数中的事了)
count++; // 费时一分钟count++
if(count > 1000) // 地图总共才10*10,所以,如果count>1000,也就是大约走了1000个点
{ // 此时大概率不会找到了,如果你不放心,那就再开大点了
out.print(0);
out.close();
System.exit(0); // 强制退出
}
}
out.println(count); // 到这说明找到了,输出count
out.close();
}
// 牛移动
public static void nYD()
{ // 这里主要是判断下一步是否符合规范
// 一、不能越界 nx+ints[lt]>=0&&nx+ints[lt]<10&&ny+ints[lt+1]>=0&&ny+ints[lt+1]<10
// 二、符合题意 chars[nx+ints[lt]][ny+ints[lt+1]]!='*'
if(nx+ints[lt]>=0&&nx+ints[lt]<10&&ny+ints[lt+1]>=0&&ny+ints[lt+1]<10&&chars[nx+ints[lt]][ny+ints[lt+1]]!='*')
{ // 如果进入该语句,说明符合题意且不越界,那就移动
nx += ints[lt];
ny += ints[lt+1];
}
else
{ // 如果进入该语句,说明无法继续沿原方向移动,那就转向
lt+=2;
lt%=8;
}
}
// 人移动
public static void rYD()
{
if(rx+ints[rlt]>=0&&rx+ints[rlt]<10&&ry+ints[rlt+1]>=0&&ry+ints[rlt+1]<10&&chars[rx+ints[rlt]][ry+ints[rlt+1]]!='*')
{
rx += ints[rlt];
ry += ints[rlt+1];
}
else
{
rlt+=2;
rlt%=8;
}
}
}
其实一开始我的读取代码使用BufferedReader类的read()方法,但是却过不了数据,下载数据后发现和答案一样,但就是无法通过,代码如下,如果有知道原因的人,还望不累赐教。
import java.io.*;
public class Main{
static char[][] chars = new char[10][10];
static int rx = 0,ry = 0;
static int nx = 0,ny = 0;
static int lt = 0;
static int rlt = 0;
static int[] ints = {-1,0,0,1,1,0,0,-1};
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
char ct;
for(int i = 0;i<10;i++)
{
for(int i1 = 0;i1<10;i1++)
{
ct = (char)ins.read();
if(ct!='n')
{
chars[i][i1] = ct;
if(ct == 'C')
{
nx = i;
ny = i1;
}
else if (ct == 'F')
{
rx = i;
ry = i1;
}
}
else
{
i1--;
}
}
}
int count = 0;
while(rx!=nx||ry!=ny)
{
nYD();
rYD();
count++;
if(count > 1000)
{
out.print(0);
out.close();
System.exit(0);
}
}
out.println(count);
out.close();
}
public static void nYD()
{
if(nx+ints[lt]>=0&&nx+ints[lt]<10&&ny+ints[lt+1]>=0&&ny+ints[lt+1]<10&&chars[nx+ints[lt]][ny+ints[lt+1]]!='*')
{
nx += ints[lt];
ny += ints[lt+1];
}
else
{
lt+=2;
lt%=8;
}
}
public static void rYD()
{
if(rx+ints[rlt]>=0&&rx+ints[rlt]<10&&ry+ints[rlt+1]>=0&&ry+ints[rlt+1]<10&&chars[rx+ints[rlt]][ry+ints[rlt+1]]!='*')
{
rx += ints[rlt];
ry += ints[rlt+1];
}
else
{
rlt+=2;
rlt%=8;
}
}
}
如有谬误,请务必告知,以免误导他人
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)