ZOJ-1834 AutoFish

AutoFish

Time Limit:  2 Seconds       Memory Limit:  65536 KB

You've built an amazing fish-catching robot, but you've discovered one small flaw: the robot can't decide whether to fish or cut bait.

You've designed a language for the Control of Oceangoing Devices (COD) which you plan to use to program the robot. COD has three instructions:

fish Fish for ten minutes. 
bait Cut bait for ten minutes. 
lunch No operation for ten minutes.

Bait is required to catch fish. The robot must cut bait for twenty minutes (execute two bait instructions) to generate a single bait unit, enough bait to catch a single fish. Other instructions (fish and lunch) may be interleaved between the two bait instructions that generate a bait unit. The robot has storage for three bait units; it cannot cut any more bait if it is already storing three bait units. If the robot is storing three bait units, a bait instruction is treated as if it were a lunch instruction (a NOP). Catching a fish consumes a bait unit.

Fish have deterministic behaviour. A fish cannot be caught more often than once every seventy minutes, and after a fish has been caught the robot must fish for thirty minutes before catching another fish (they get shy). In order to successfully complete a fish instruction, the robot must have a least one bait unit. If the robot has no bait, a fish instruction cannot be successfully completed and is treated as if it were a lunch instruction. When the robot first starts fishing, a fish is caught on the first fish instruction that completes (beginner's luck). If at least one fish has already been caught then a fish is caught on the completion of a fish instruction if and only if: 1) the fish instruction is at least the seventh COD instruction executed since the last instruction on which a fish was caught, and 2) the fish instruction is at least the third fish instruction successfully completed since the last instruction on which a fish was caught.

The execution of a lunch instruction allows time to pass, but has no other purpose.

The robot starts with no bait; no fish have been caught.


Input

Input consists of a sequence of fish, bait and lunch instructions, terminated by a blank line.


Output

Output for each case is a line containing a single integer, indicating the number of fish the robot has caught at the end of the sequence.


Sample Input

fish
fish
lunch
bait
fish
bait
fish
bait
bait
fish
fish
fish
fish
lunch
lunch
lunch
lunch
fish
fish
fish


Sample Output

2



在最后输出时由于输入结束后没有空行输入,所以当最后一个数据结束后需要输出结果


#include <stdio.h>
#include <string.h>

int main()
{
	int fishs, time, bait, t, b;
	char command[10];
	fishs = 0;
	time = 0;
	bait = 0;
	t = 0;
	b = 0;
	while(gets(command) != NULL) {
		if(strcmp(command, "") == 0) {
			printf("%d\n", fishs);
			time = 0;
			fishs = 0;
			bait = 0;
			t = 0;
			b = 0;
			continue;
		}
		if(strcmp(command, "fish") == 0) {
			if(fishs) {
				time++;
			}
			if(bait) {
				if(fishs) {
					t++;
				}
			}
			if(!fishs) {
				if(bait) {
					bait--;
					fishs++;
					time = 0;
					t = 0;
				}
			} else {
				if(time >= 7 || t >= 3) {
					if(bait) {
						bait--;
						fishs++;
						time = 0;
						t = 0;
					}
				}
			}
		}
		else if(strcmp(command, "bait") == 0) {
			if(fishs) {
				time++;
			}
			if(bait < 3) {
				b++;
			}
			if(b == 2) {
				b = 0;
				if(bait < 3) {
					bait++;
				}
			}
		}
		else if(strcmp(command, "lunch") == 0) {
			if(fishs) {
				time++;
			}
		}
	}
	printf("%d\n", fishs);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值