24h購物| | PChome| 登入
2013-02-23 13:55:25| 人氣1,606| 回應0 | 上一篇 | 下一篇

[UVA][dfs][關燈] 10309 - Turn the Lights Off

推薦 0 收藏 0 轉貼0 訂閱站台

Problem J

Turn the Lights Off

Input: standard input

Output: standard output

Time Limit: 3 seconds

Memory Limit: 32 MB

 

Since we all rely on mother earth, it's our duty to save her. Therefore, you're now asked to save energy by switching lights off.

A friend of yours has the following problem in his job. There's a grid of size 10x10, where each square has a light bulb and a light switch attached to it. Unfortunately, these lights don't work as they are supposed to. Whenever a switch is pressed, not only its own bulb is switched, but also the ones left, right, above and under it. Of course if a bulb is on the edge of the grid, there are fewer bulbs switched.

When a light switches it means it's now on if it was off before and it's now off if it was on before. Look at the following examples, which show only a small part of the whole grid. They show what happens if the middle switch is pressed. "O" stands for a light that's on, "#" stands for a light that's off.

###      #O#
###  ->  OOO
###      #O#
 
###      #O#
OOO  ->  ###
###      #O#

Your friend loves to save energy and asks you to write a program that finds out if it is possible to turn all the lights off and if possible then how many times he has to press switches in order to turn all the lights off.

Input

There are several test cases in the input. Each test case is preceded by a single word that gives a name for the test case. After that name there follow 10 lines, each of which contains a 10-letter string consisting of "#" and "O". The end of the input is reached when the name string is "end".

Output

For every test case, print one line that consists of the test case name, a single space character and the minimum number of times your friend has to press a switch. If it is not possible to switch off all the lights or requires more than 100 presses then the case name should be followed by space and then a -1.

Sample Input

all_off
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
all_on
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
simple
#O########
OOO#######
#O########
####OO####
###O##O###
####OO####
##########
########O#
#######OOO
########O#
end

 

Sample Output

all_off 0
all_on 44
simple 4

(The Joint Effort Contest, Problem setter: Stefan Pochmann)

 

#include <stdio.h>
#include <string.h>
char g[15][15];
int ng[15][15], res;
void press(int x, int y) {
    ng[x][y] = !ng[x][y];
    if(x-1 >= 0)    ng[x-1][y] = !ng[x-1][y];
    if(x+1 < 10)    ng[x+1][y] = !ng[x+1][y];
    if(y-1 >= 0)    ng[x][y-1] = !ng[x][y-1];
    if(y+1 < 10)    ng[x][y+1] = !ng[x][y+1];
}
void dfs(int x, int y, int step) {
    if(y == 10)  x++, y = 0;
    if(step >= res) return;
    if(x == 0) {
        dfs(x, y+1, step);
        press(x, y);
        dfs(x, y+1, step+1);
        press(x, y);
    } else if(x < 10) {
        if(ng[x-1][y] == 1) {
            press(x, y);
            dfs(x, y+1, step+1);
            press(x, y);
        } else
            dfs(x, y+1, step);
    } else {
        int i;
        for(i = 0; i < 10; i++)
            if(ng[x-1][i])
                return;
        if(step < res)  res = step;
    }
}
int main() {
    char name[50];
    int i, j;
    while(gets(name)) {
        if(!strcmp(name, "end"))
            break;
        for(i = 0; i < 10; i++)
            gets(g[i]);
        for(i = 0; i < 10; i++) {
            for(j = 0; j < 10; j++) {
                ng[i][j] = g[i][j] == 'O';
            }
        }
        res = 0xffff;
        dfs(0, 0, 0);
        printf("%s %d\n", name, res);
    }
    return 0;
}
/*
all_off
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
all_on
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
simple
#O########
OOO#######
#O########
####OO####
###O##O###
####OO####
##########
########O#
#######OOO
########O#
*/

台長: Morris
人氣(1,606) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][字串處理][bfs] 10044 - Erdos Numbers
此分類上一篇:[UVA][dp] 10532 - Combination! Once Again

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文