poj 1166 The Clocks (暴力|| dfs)

27 篇文章 0 订阅
23 篇文章 0 订阅

The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16196 Accepted: 6627

Description

|-------|    |-------|    |-------|

|       |    |       |    |   |   |

|---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)

Input

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.

Output

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.

Sample Input

3 3 0
2 2 2
2 1 2

Sample Output

4 5 8 9


分析:每一个时钟最多转3次,所以,枚举一下,4^9次方,用dfs,最后判断是否符合即可,把数量最少的存一下。网上还有优化的算法4^4

代码如下:

#include <stdio.h>
int a[10];
int b[10];
int fin[1005];
int min = 1000;
int add[9][9] = {
	//A,B,C,D,E,F,G,H,I
	{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}
};

void dfs(int t)
{
	int i, j, k;
	if (t == 9)
	{
		int temp[10], count=0;
		for (i = 0; i < 9; i++)
			temp[i] = a[i];
		for (i = 0; i < 9; i++)
		for (k = 0; k < b[i]; k++)
		for (j = 0; j < 9; j++)
		{
			temp[j] = (temp[j] + add[i][j]) % 4;
			count++;
		}
		int sum = 0;
		for (i = 0; i < 9; i++)
			sum += temp[i];
		if (sum == 0 && count/9 < min)
		{
			min = 0;
			for (i = 0; i < 9; i++)
			{
				for (j = 0; j < b[i]; j++)
				{
					fin[min++] = i+1;
				}
			}
		}
		return ;	
	}
	for (i = 0; i < 4; i++)
	{
		b[t] = i;
		dfs(t + 1);
	}

}

int main()
{
	int i,j;
	//freopen("input.txt","r",stdin);
	for (i = 0; i < 9; i++)
		scanf("%d",a+i);
	dfs(0);
	for (i = 0; i < min; i++)
		printf("%d ", fin[i]);
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值