POJ 1166:The Clocks

总时间限制: 
1000ms
内存限制: 
65536kB
描述
|-------|    |-------|    |-------|
|       |    |       |    |   |   |
|---O   |    |---O   |    |   O   |
|       |    |       |    |       |
|-------|    |-------|    |-------|
    A            B            C    

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

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O---|    |   O   |
|   |   |    |       |    |   |   |
|-------|    |-------|    |-------|
    G            H            I    
(Figure 1)

There are nine clocks in a 3*3 array (figure 1). The goal is to return all the dials to 12 o'clock with as few moves as possible. There are nine different allowed ways to turn the dials on the clocks. Each such way is called a move. Select for each move a number 1 to 9. That number will turn the dials 90' (degrees) clockwise on those clocks which are affected according to figure 2 below. 

Move   Affected clocks
 
 1         ABDE
 2         ABC
 3         BCEF
 4         ADG
 5         BDEFH
 6         CFI
 7         DEGH
 8         GHI
 9         EFHI    
   (Figure 2)
输入
Your program is to read from standard input. Nine numbers give the start positions of the dials. 0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock.
输出
Your program is to write to standard output. Output a shortest sorted sequence of moves (numbers), which returns all the dials to 12 o'clock. You are convinced that the answer is unique.
样例输入
3 3 0
2 2 2
2 1 2

样例输出

4 5 8 9

穷举方法,代码写的有点麻烦,为了节约时间,每个循环下都判断一下如果当前步数已经超过最小步数就放弃,返回上层循环。

#include <iostream>
using namespace std;
int moves[9][9]={{1,1,0,1,1,0,0,0,0},{1,1,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0},
                {1,0,0,1,0,0,1,0,0},{0,1,0,1,1,1,0,1,0},{0,0,1,0,0,1,0,0,1},
				{0,0,0,1,1,0,1,1,0},{0,0,0,0,0,0,1,1,1},{0,0,0,0,1,1,0,1,1}};
int m[9];
int input[9];
int output[9];
int temp[9];
bool isOK(){
	for(int i=0;i<9;i++){
		temp[i]=input[i]+m[0]*moves[0][i]+m[1]*moves[1][i]+m[2]*moves[2][i]+m[3]*moves[3][i]+
		m[4]*moves[4][i]+m[5]*moves[5][i]+m[6]*moves[6][i]+m[7]*moves[7][i]+m[8]*moves[8][i];
		temp[i]=temp[i]%4;
		if(temp[i]!=0){
			return false;
		}
	}
	return true;
}
void result(){
	int temp[100];
	int k=0;
	for(int i=0;i<9;i++){
		for(int j=0;j<output[i];j++){
			temp[k]=i+1;
			k++;
		}
	}
	for(int i=0;i<k-1;i++){
		cout<<temp[i]<<" ";
	}
	cout<<temp[k-1];
}
int summ(){
	return (m[0]+m[1]+m[2]+m[3]+m[4]+m[5]+m[6]+m[7]+m[8]);
}
int main(){
	for(int i=0;i<9;i++){
		cin>>input[i];
	}
	int minn=100000;
	for(m[0]=0;m[0]<4;m[0]++){
		for(m[1]=0;m[1]<4;m[1]++){
			for(m[2]=0;m[2]<4;m[2]++){
				if(m[0]+m[1]>minn){
					break;
				}
				for(m[3]=0;m[3]<4;m[3]++){
					if(m[0]+m[1]+m[2]+m[3]>minn){
						break;
					}
					for(m[4]=0;m[4]<4;m[4]++){
						if(m[0]+m[1]+m[2]+m[3]+m[4]>minn){
							break;
						}
						for(m[5]=0;m[5]<4;m[5]++){
							if(m[0]+m[1]+m[2]+m[3]+m[4]+m[5]>minn){
								break;
							}
							for(m[6]=0;m[6]<4;m[6]++){
								if(m[0]+m[1]+m[2]+m[3]+m[4]+m[5]+m[6]>minn){
									break;
								}
								for(m[7]=0;m[7]<4;m[7]++){
									if(m[0]+m[1]+m[2]+m[3]+m[4]+m[5]+m[6]+m[7]>minn){
									break;
								}
									for(m[8]=0;m[8]<4;m[8]++){
										if(isOK()){
											if(summ()<minn){
												minn=summ();
												for(int ii=0;ii<9;ii++){
													output[ii]=m[ii];
												}
											}
											else{
												break;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	result();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_chen_l/article/details/80874782