[UVA][搜索bitmask] 10318 - Security Panel@Morris' Blog|PChome Online 人新台
2013-08-23 20:30:31| 人4,554| 回0 | 上一篇 | 下一篇

[UVA][搜索bitmask] 10318 - Security Panel

0 收藏 0 0 站台

Problem G

Security Panel

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

Advanced Control Mechanisms (ACM) produces sophisticated electronic locks and security devices.

The company's most recent invention is a panel of illuminated buttons in r rows and c columns. The buttons are numbered left-to-right, top-to-bottom, starting at 1 in the upper-left corner. Each button has two states: lit and unlit. Initially, all buttons are unlit. Pressing a button switches the state of some buttons from lit to unlit (or vice-versa) according to a 3x3 pattern. Pressing a button on a panel applies the pattern centered on that button. To unlock the panel, the buttons must be pressed in such a way so as to light all of them.

For example, consider the following pattern where pressing a button switches the state of the button pressed, as well as the button above and the buttons to the upper and lower left.


 

If we use this pattern on a 2x3 panel, then pressing buttons 2, 5, and 6 will light all the buttons. If pressed in that order, the state changes of the panel are: (Light green means lights are off and vice versa)

 

 

Input

Each input case will begin with the number of rowsand columns on the panel, 1 <= r, c <= 5 alone on a line. The next three lines describe how pressing a button will affect the nearby lights. This description consists of a 3x3 character grid, where the character "*" indicates that the light in that position switches state (from lit to unlit or from unlit to lit) while "." means its state remains unchanged.

Input ends with 0 0 alone on a line.


Output

For each input case, output "Case #" followed by the number of the case. If there is no way to turn on all the lights, print "Impossible." If it is possible to turn on the lights, output the buttons to be pressed in increasing order, separated by single space. Output the answer that requires the fewest number of buttons possible to be pressed. If there is more than one correct solution, anyone will do.

 

Sample Input

2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0

 

Sample Output

Case #1
2 5 6
Case #2
2 3 4 7 9 12 14 17 18 19 
Case #3
1 3 
Case #4
Impossible.

(The Decider Contest, Source: University of Alberta Local Contest)

目似於,但是的影限定十字。

而定影的情,有有可能全成全。

首先最大只有 5x5,考成一 int,之後的反使用 XOR 算即可。

同理每一格下的遮罩,搜索可能高 O(2^25),

依序,前排的必然已全了,藉此作剪枝。


#include <stdio.h>
#include <algorithm>
using namespace std;
int mark[10][10];
int row[10];
int n, m;
int ch[105], found;
void dfs(int x, int y, int state, int step) {
    if(found)   return;
    if(y == m) {
        if(x) {
            if((state&row[x-1]) != row[x-1])
                return;
        }
        x++, y = 0;
    }
    int i, j;
    /*for(i = 0; i < n; i++, puts(""))
        for(j = 0; j < m; j++)
            printf("%d", (state>>(i*5+j))&1);
    printf("-------%d %d %d\n", x, y, step);*/
    if(x == n) {
        if((state&row[x-1]) != row[x-1])
            return;
        found = 1;
        for(i = 0; i < step; i++) {
            if(i)   putchar(' ');
            printf("%d", ch[i]+1);
        }
        puts("");
        return;
    }
    dfs(x, y+1, state, step);
    ch[step] = x*m+y;
    dfs(x, y+1, state^mark[x][y], step+1);
}
int main() {
    /*freopen("in.txt","r+t",stdin);
    freopen("out2.txt","w+t",stdout);*/
    char g[5][5];
    int i, j, k, p, q, r;
    int a, b;
    int cases = 0;
    while(scanf("%d %d", &n, &m) == 2 && n) {
        for(i = 0; i < 3; i++)
            scanf("%s", g[i]);
        int x, y;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                int &val = mark[i][j];
                val = 0;
                for(p = 0, a = -1; p < 3; p++, a++) {
                    for(q = 0, b = -1; q < 3; q++, b++) {
                        x = i+a, y = j+b;
                        if(x < 0 || y < 0 || x >= n || y >= m)
                            continue;
                        if(g[p][q] == '*')
                            val |= 1<<(x*5+y);
                    }
                }
            }
        }
        /*for(i = 0; i < n; i++, puts(""))
            for(j = 0; j < m; j++)
                printf("%d ", mark[i][j]);*/
        for(i = 0; i < n; i++) {
            row[i] = 0;
            for(j = 0; j < m; j++)
                row[i] |= 1<<(i*5+j);
        }
        found = 0;
        printf("Case #%d\n", ++cases);
        dfs(0, 0, 0, 0);
        if(found == 0)
            puts("Impossible.");
    }
    return 0;
}
/*
2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0
*/


台: Morris
人(4,554) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA] 11065 - A Gentlemen's Agreement
此分上一篇:[UVA][dp] 1052 - Bit Compressor

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86