Input
The input consists of several test cases. The first line of each case contains a single integer n(1<=n<=10), the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers a;b;c(1<=a;b<=5), described above. The last test case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and the first time all the students are awaken. If it'll never happen, output `-1'.
SampleInput
3
2 4 1
1 5 2
1 4 3
3
1 2 1
1 2 2
1 2 3
0
SampleOutput
Case 1: 18
The input consists of several test cases. The first line of each case contains a single integer n(1<=n<=10), the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers a;b;c(1<=a;b<=5), described above. The last test case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and the first time all the students are awaken. If it'll never happen, output `-1'.
SampleInput
3
2 4 1
1 5 2
1 4 3
3
1 2 1
1 2 2
1 2 3
0
SampleOutput
Case 1: 18
Case 2: -1
分析:刚开始感觉无从下手,如果把数据处理好就容易多了:开辟一个二维数组,每维表示一个学生的状态,每维第一位是状态总数,接下来是a个醒(用1表示)和b个睡(用0表示),后面跟两个状态标志量,第一个标志是当前状态,第二个是最初状态; 处理过程:先扫描每个学生,看看活着的有几个,然后对每个学生进行状态转移,转移完了就判断活着的人数以及转移后的状态和最初的状态是不是一样(如果一样说明回到最初的状态,则是死循环,可以判断无法全部活着),如果全活着就输出,不是就继续。
#include <stdio.h>
#include <string.h>
int student[10][15];
int main()
{
int kase = 0;
int stu_n;
while(~scanf("%d",&stu_n)&&stu_n)
{
int a,b,c,current;
for(int i=0; i<stu_n; i++)
{
scanf("%d%d%d",&a,&b,&c);
student[i][0] = a+b;
current = a+b;
for(int j=1; j<=a; j++)
student[i][j] = 1;
for(int j=1; j<=b; j++)
student[i][a+j] = 0;
student[i][current+1] = c;
student[i][current+2] = c;
}
// for(int i=0; i<stu_n; i++)
// {
// for(int j=0; j<=student[i][0]+2; j++)
// printf("%3d",student[i][j]);
// putchar('\n');
// }
// continue;
int state;
int hour = 1;
printf("Case %d:",++kase);
while(1)
{
state = 0;
int alive = 0;
for(int i=0; i<stu_n; i++)
{
int current_index = student[i][0];
int current_state = student[i][current_index+1];
if(student[i][current_state]==1)alive++;
}
if(alive<stu_n-alive)state=1;
else if(alive >= stu_n){printf(" %d\n",hour);break;}
int allalive = 0;
int allold = 0;
for(int i=0; i<stu_n; i++)
{
int current_index = student[i][0];
int current_state = student[i][current_index+1];
if(!state && student[i][current_state]==1 && student[i][current_state%current_index+1]==0)
student[i][current_index+1] = 1;
else student[i][current_index+1] = current_state%current_index+1;
if(student[i][current_index+1] == student[i][current_index+2])allold++;
current_state = student[i][current_index+1];
if(student[i][current_state] == 1)allalive++;
}
hour++;
if(allalive==stu_n){printf(" %d\n",hour);break;}
if(allold==stu_n){printf(" -1\n");break;}
}
}
return 0;
}