USACO-The Tamworth Two
来源:互联网 发布:侣行为什么停播知乎 编辑:程序博客网 时间:2024/05/07 03:05
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:
an obstacle,
the cows (who always travel together), or
Farmer John.
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 is
represented
as follows:
. Empty square
* Obstacle
C Cows
F Farmer
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
题意:牛和人只能往前走,遇到障碍和边缘时就顺时针旋转90度,旋转和往前走都要花费1时间,问经过多长时间牛和人会相遇。
模拟题,每一次可能有上下左右和不走5种情况,最多只有500*500步,如果超过了还没有相遇就不可能相遇了,因为之后的一定会是重复的路线。
代码:
/*ID:iam666PROG:ttwoLANG:C++*/#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>using namespace std;int main (void){ freopen("ttwo.in","r",stdin); freopen("ttwo.out","w",stdout); int fx,fy,cx,cy; int ob[11][11],dc=0,df=0; int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; memset(ob,0,sizeof(ob)); for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { char c; cin>>c; if(c=='*') ob[i][j]=1; if(c=='F') { fx=i,fy=j; } if(c=='C') { cx=i,cy=j; } } } int time=0,x,y; while(1) { if(fx==cx&&fy==cy) break; x=fx+go[df][0]; y=fy+go[df][1]; if((ob[x][y]==1)||(x<1)||x>10||y<1||y>10) df=(df+1)%4; else { fx=x; fy=y; } x=cx+go[dc][0]; y=cy+go[dc][1]; if((ob[x][y]==1)||(x<1)||x>10||y<1||y>10) dc=(dc+1)%4; else { cx=x; cy=y; } time++; if(time>250000) break; } if(time<=250000) printf("%d\n",time); else printf("0\n"); return 0;}
- usaco-The Tamworth Two
- USACO The Tamworth Two
- usaco The Tamworth Two
- usaco:The Tamworth Two
- USACO-The Tamworth Two
- USACO The Tamworth Two
- USACO-The Tamworth Two
- 【其他】【USACO】The Tamworth Two
- USACO--2.4The Tamworth Two
- USACO 2.4 The Tamworth Two
- usaco The Tamworth Two 两只塔姆沃斯牛
- USACO :The Tamworth Two解题报告
- USACO 2.4.1 The Tamworth Two
- USACO 2.4 The Tamworth Two (ttwo)
- USACO 2.4.1 The Tamworth Two
- usaco Tamworth Two
- USACO section 2.4 The Tamworth Two(算模拟吧)
- USACO section2.4 The Tamworth Two题解&代码
- Web开发笔记(5):第一个JSP+javascript例子
- Real-Time Rendering 翻译 3.图形处理单元
- ubuntu 编译安装 Nginx
- 操作系统是如何利用中断机制的
- iOS_PDf
- USACO-The Tamworth Two
- 关于Logistics回归用于分类的解释
- MFC实现位图超链接代码
- leetcode题解汇总
- C++ getline()函数的用法
- 16位汇编语言寄存器及指令整理
- 计划~
- 我的第一篇CSDN博客文章,Python代码实现矩阵翻转
- LeetCode 359. Logger Rate Limiter(日志漏桶)