usaco_ttwo
BIO '98 - Richard Forster
A pair of cows is loose somewhere in the forest. Farmer John is lending his expertise to their capture. Your task is to model their behavior.
The chase takes place on a 10 by 10 planar grid. Squares can be empty or they can contain:
The cows and Farmer John can occupy the same square (when they `meet') but neither the cows nor Farmer John can share a square with an obstacle.
Each square isrepresentedas follows:
Here is a sample grid:
*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......
The cows wander around the grid in a fixed way. Each minute, they either move forward or rotate. Normally, they move one square in the direction they are facing. If there is an obstacle in the way or they would leave the board by walking `forward', then they spend the entire minute rotating 90 degrees clockwise.
Farmer John, wise in the ways of cows, moves in exactly the same way.
The farmer and the cows can be considered to move simultaneously during each minute. If the farmer and the cows pass each other while moving, they are not considered to have met. The chase ends when Farmer John and the cows occupy the same square at the end of a minute.
Read a ten-line grid that represents the initial state of the cows, Farmer John, and obstacles. Each of the ten lines contains exactly ten characters using the coding above. There is guaranteed to be only one farmer and one pair of cows. The cows and Farmer John will not initially be on the same square.
Calculate the number of minutes until the cows and Farmer John meet. Assume both the cows and farmer begin the simulation facing in the `north' direction. Print 0 if they will never meet.
PROGRAM NAME: ttwo
INPUT FORMAT
Lines 1-10:
Ten lines of ten characters each, as explained above
SAMPLE INPUT (file ttwo.in)
*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......
OUTPUT FORMAT
A single line with the integer number of minutes until Farmer John and the cows meet. Print 0 if they will never meet.
SAMPLE OUTPUT (file ttwo.out)
49
/*
ID: guangxue1
PROG: ttwo
LANG: C++
*/
#include <fstream>
using namespace std;
char map[10][10];
int Fx,Fy,Cx,Cy;
int decF,decC;
void moveF()
{
switch(decF)
{
case 0: --Fy; break;
case 1: ++Fx; break;
case 2: ++Fy; break;
case 3: --Fx;
}
}
void moveC()
{
switch(decC)
{
case 0: --Cy; break;
case 1: ++Cx; break;
case 2: ++Cy; break;
case 3: --Cx;
}
}
void Fdec()
{
if((Fy==0||map[Fx][Fy-1]=='*')&&decF==0||(Fx==9||map[Fx+1][Fy]=='*')&&decF==1||(Fy==9||map[Fx][Fy+1]=='*')&&decF==2||(Fx==0||map[Fx-1][Fy]=='*')&&decF==3)
{++decF; decF%=4;}
else
moveF();
}
void Cdec()
{
if((Cy==0||map[Cx][Cy-1]=='*')&&decC==0||(Cx==9||map[Cx+1][Cy]=='*')&&decC==1||(Cy==9||map[Cx][Cy+1]=='*')&&decC==2||(Cx==0||map[Cx-1][Cy]=='*')&&decC==3)
{++decC; decC%=4;}
else
moveC();
}
bool F()
{
if(Fx==Cx&&Fy==Cy)
return true;
return false;
}
int main()
{
ifstream fin("ttwo.in");
ofstream fout("ttwo.out");
for(int i=0; i<10; ++i)
for(int j=0; j<10; ++j)
{
fin>>map[j][i];
if(map[j][i]=='F')
Fx=j, Fy=i;
else if(map[j][i]=='C')
Cx=j, Cy=i;
}
int time;
for( time=0; time<10000; ++time)
{
Fdec();
Cdec();
if(F()) {fout<<time+1<<endl; break;}
}
if(time==10000) fout<<0<<endl;
return 0;
}
:
The Tamworth Two
Russ Cox
We can simply simulate the movement of Farmer John and the two cows, but we need to find a way to detect loops. There are only 100 places where the cows can be, and 4 directions they can face. The same goes for Farmer John. This multiplies out to 400*400 = 160,000 different (place, direction) configurations for Farmer John and the cows. If we ever happen upon a configuration we have been in before, we are in an infinite loop, and John and the cows will never meet.
In fact, we don't even need to keep track of which configurations we've seen. If they're going to meet, they're going to meet in fewer than 160,000 steps. Otherwise, they have to repeat some configuration, in which case they'll never meet.
We take care of the simulation by keeping track of the position and direction of each. Direction is a number from 0 to 3, 0 = north, 1 = east, 2 = south, 3 = west. So turning clockwise is incrementing one. We calculate how to move given the direction by looking up offsets in the deltax and deltay arrays.
/*
PROG: ttwo
ID: rsc001
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
char grid[10][10];
/* delta x, delta y position for moving north, east, south, west */
int deltax[] = { 0, 1, 0, -1 };
int deltay[] = { -1, 0, 1, 0 };
void
move(int *x, int *y, int *dir)
{
int nx, ny;
nx = *x+deltax[*dir];
ny = *y+deltay[*dir];
if(nx < 0 || nx >= 10 || ny < 0 || ny >= 10 || grid[ny][nx] == '*')
*dir = (*dir + 1) % 4;
else {
*x = nx;
*y = ny;
}
}
void
main(void)
{
FILE *fin, *fout;
char buf[100];
int i, x, y;
int cowx, cowy, johnx, johny, cowdir, johndir;
fin = fopen("ttwo.in", "r");
fout = fopen("ttwo.out", "w");
assert(fin != NULL && fout != NULL);
cowx = cowy = johnx = johny = -1;
for(y=0; y<10; y++) {
fgets(buf, sizeof buf, fin);
for(x=0; x<10; x++) {
grid[y][x] = buf[x];
if(buf[x] == 'C') {
cowx = x;
cowy = y;
grid[y][x] = '.';
}
if(buf[x] == 'F') {
johnx = x;
johny = y;
grid[y][x] = '.';
}
}
}
assert(cowx >= 0 && cowy >= 0 && johnx >= 0 && johny >= 0);
cowdir = johndir = 0; /* north */
for(i=0; i<160000 && (cowx != johnx || cowy != johny); i++) {
move(&cowx, &cowy, &cowdir);
move(&johnx, &johny, &johndir);
}
if(cowx == johnx && cowy == johny)
fprintf(fout, "%d/n", i);
else
fprintf(fout, "0/n");
exit(0);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java에서 InputStream, String, File 간의 상호 전환 비교InputStream, String, File 상호 전환 1. String --> InputStream 2. InputStream --> String 오늘 인터넷에서 또 다른 방법을 보았는데, 특별히 가지고 와서 공...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.