您的位置:首页 > 其它

POJ 3185  The Water Bowls

2012-12-05 17:09 447 查看
The Water Bowls

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 2671Accepted: 991
Description

The cows have a line
of 20 water bowls from which they drink. The bowls can be either
right-side-up (properly oriented to serve refreshing cool water) or
upside-down (a position which holds no water). They want all 20
water bowls to be right-side-up and thus use their wide snouts to
flip bowls.

Their snouts, though, are so wide that they flip not only one bowl
but also the bowls on either side of that bowl (a total of three or
-- in the case of either end bowl -- two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable --
it even looks like a bowl), what is the minimum number of bowl
flips necessary to turn all the bowls right-side-up?
Input

Line 1: A single
line with 20 space-separated integers
Output

Line 1: The minimum
number of bowl flips necessary to flip all the bowls right-side-up
(i.e., to 0). For the inputs given, it will always be possible to
find some combination of flips that will manipulate the bowls to 20
0's.
Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

Sample Output

3

Hint

Explanation of the
sample:

Flip bowls 4, 9, and 11 to make them all drinkable:

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]

0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl
4]

0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl
9]

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl
11]
Source

USACO 2006 January Bronze

这个题实际上和POj 1753 Flip
Game(http://poj.org/problem?id=1753)是一样的,而且比那个题还容易了一些,也是看了半天才弄明白,首先题目上说明最小步数保证小于20,那么我们就可以按步数从0到20枚举,然后深搜(其实我也不明白到底是深搜还是广搜,弄迷了),一旦有一个符合条件即退出,这样得到的步数就一定是最小步数了,废话少说,直接贴代码和测试数据:

#include<stdio.h>

int num[21],step,flag;

int range()

{

    int i;

   
for(i=0;i<20;i++)

   
   
if(num[i]==1)

   
   
    return
0;

    return
1;

}

void turn(int i)

{

   
num[i]=!num[i];

   
if(i>0)

   
   
num[i-1]=!num[i-1];

   
if(i<19)num[i+1]=!num[i+1];

}

void DFS(int i,int dp)

{

   
if(step==dp){

   
   
flag=range();

   
   
return;

    }

   
if(i>=20||flag)return;

   
turn(i);

   
DFS(i+1,dp+1);

   
turn(i);

   
DFS(i+1,dp);

}

int main()

{

    int i;

   
for(i=0;i<20;i++)

   
   
scanf("%d",&num[i]);

   
for(step=0;step<20;step++)

    {

   
   
flag=0;

   
   
DFS(0,0);

   
   
if(flag)break;

    }

   
printf("%d\n",step);

    return
0;

}
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 03
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

2

0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0

1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0

1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: