The clocks


The Clocks
IOI'94 - Day 2
Consider nine clocks arranged in a 3x3 array thusly:
|-------|    |-------|    |-------|    
|       |    |       |    |   |   |    
|---O   |    |---O   |    |   O   |          
|       |    |       |    |       |           
|-------|    |-------|    |-------|    
    A            B            C

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O   |    |   O   |
|   |   |    |   |   |    |   |   |
|-------|    |-------|    |-------|
    D            E            F

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O---|    |   O   |
|   |   |    |       |    |   |   |
|-------|    |-------|    |-------|
    G            H            I

The goal is to find a minimal sequence of moves to return all the dials to 12 o'clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.

MoveAffected clocks
1ABDE
2ABC
3BCEF
4ADG
5BDEFH
6CFI
7DEGH
8GHI
9EFHI

Example

Each number represents a time accoring to following table:
9 9 12       9 12 12       9 12 12        12 12 12      12 12 12 
6 6 6  5 ->  9  9  9  8->  9  9  9  4 ->  12  9  9  9-> 12 12 12 
6 3 6        6  6  6       9  9  9        12  9  9      12 12 12 

[But this might or might not be the `correct' answer; see below.]

PROGRAM NAME: clocks

INPUT FORMAT

Lines 1-3:Three lines of three space-separated numbers; each number represents the start time of one clock, 3, 6, 9, or 12. The ordering of the numbers corresponds to the first example above.

SAMPLE INPUT (file clocks.in)

9 9 12
6 6 6
6 3 6

OUTPUT FORMAT

A single line that contains a space separated list of the shortest sequence of moves (designated by numbers) which returns all the clocks to 12:00. If there is more than one solution, print the one which gives the lowest number when the moves are concatenated (e.g., 5 2 4 6 < 9 3 1 1).

SAMPLE OUTPUT (file clocks.out)

4 5 8 9



妈的原来是标题写错了。。。是clocks!!有个s握艹


/*
ID: des_jas1
PROG: clocks
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
//#define fin cin
//#define fout cout
using namespace std;

const int MAX=10000;
int N=MAX,used[10],cl[9],step[MAX],Path[MAX];

void Move(int p)
{
	switch(p)
	{
	case 1:
		{
			cl[0]=(cl[0]+3)%4;cl[1]=(cl[1]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;
			break;
		}
	case 2:
		{
			cl[0]=(cl[0]+3)%4;cl[1]=(cl[1]+3)%4;cl[2]=(cl[2]+3)%4;
			break;
		}
	case 3:
		{	
		    cl[1]=(cl[1]+3)%4;cl[2]=(cl[2]+3)%4;cl[5]=(cl[5]+3)%4;cl[4]=(cl[4]+3)%4;
			break;
		}
	case 4:
		{
			cl[0]=(cl[0]+3)%4;cl[6]=(cl[6]+3)%4;cl[3]=(cl[3]+3)%4;
			break;
		}
	case 5:
		{
			cl[5]=(cl[5]+3)%4;cl[1]=(cl[1]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;cl[7]=(cl[7]+3)%4;
			break;
		}
	case 6:
		{
			cl[2]=(cl[2]+3)%4;cl[5]=(cl[5]+3)%4;cl[8]=(cl[8]+3)%4;
			break;
		}
	case 7:
		{
			cl[6]=(cl[6]+3)%4;cl[7]=(cl[7]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;
			break;
		}
	case 8:
		{
			cl[6]=(cl[6]+3)%4;cl[7]=(cl[7]+3)%4;cl[8]=(cl[8]+3)%4;
			break;
		}
	case 9:
		{
			cl[5]=(cl[5]+3)%4;cl[4]=(cl[4]+3)%4;cl[7]=(cl[7]+3)%4;cl[8]=(cl[8]+3)%4;
			break;
		}
	}


}

bool IS_End()
{
	int i;
	for(i=0;i<9;i++)
		if(cl[i])
			return false;
	return true;
}

void Trans(int n)
{
	if(n>=N) //可以“=”因为是升序
	    return;
        int i;
	if(IS_End())
	{
	   N=n;
	   for(i=0;i<N;i++)
               Path[i]=step[i];
	}
	else
	{
		if(!n)
			i=1;
		else
			i=step[n-1];
		for(;i<=9;i++)
		{
			if(used[i]<3)
			{
			    int j;
				Move(i);
				step[n]=i;
				used[i]++;
				Trans(n+1);
				used[i]--;//老忘 
				for(j=0;j<3;j++) //移动4次相当于没移动
					Move(i);
			}
		}
	}
	return;
}

int main() 
{
	ofstream fout ("clocks.out");
    ifstream fin ("clocks.in");
	int i,tp;
   	for(i=0;i<9;i++)
	{
 		fin>>tp; 
		cl[i]=(12-tp)/3;
	}
	memset(used,0,sizeof(used));
	memset(step,1,sizeof(step));
	memset(Path,0,sizeof(Path));
	Trans(0);
	fout<<Path[0];
	for(i=1;i<N;i++)
		fout<<" "<<Path[i];
	fout<<endl;
	fout.close();
	fin.close();
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值