ural1060

http://hi.baidu.com/raulliubo/blog/item/d07461f4c78417ddf2d385b3.html

ID Date Author Problem Language Judgement result Test # Execution time Memory used
1951467 12:39:21
7 Feb 2008
raulliubo 1060 Pascal Accepted 0.031 135 KB
一道很弱的题,dfs都这么过了。ms枚举也可以,bfs我快忘了怎么写了。。看来该练练了。

my ugly code:

const
    dir : array[1 .. 5, 1 .. 2] of integer = ((0, 1), (0, -1), (1, 0), (-1, 0), (0, 0));

var
    map, v : array[1 .. 4, 1 .. 4] of boolean;
    min, i, j : longint;
    find : boolean;
    ch : char;

function check : boolean;
var
    i, j : longint;
    flag : boolean;
begin
    flag := true;
    for i := 1 to 4 do begin
        for j := 1 to 4 do
            if map[i, j] then begin
                flag := false;
                break;
            end;
        if not flag then break;
    end;
    if flag then exit(true);
    flag := true;
    for i := 1 to 4 do begin
        for j := 1 to 4 do
            if not map[i, j] then begin
                flag := false;
                break;
            end;
        if not flag then break;
    end;
    if flag then exit(true);
    exit(false);
end;

procedure change(x, y : longint);
var
    tx, ty, i : longint;
begin
    for i := 1 to 5 do begin
        tx := x + dir[i, 1];
        ty := y + dir[i, 2];
        if (tx >= 1) and (ty >= 1) and (tx <= 4) and (ty <= 4) then
            map[tx, ty] := not map[tx, ty];
    end;
end;

function tot : longint;
var
    t, i, j : longint;
begin
    t := 0;
    for i := 1 to 4 do
        for j := 1 to 4 do
            if v[i, j] then inc(t);
    exit(t);
end;

procedure go(x, y : longint);
begin
    if x > 4 then begin
        if check then begin
            if tot < min then
                min := tot;
            find := true;
        end;
        exit;
    end;
    change(x, y);
    v[x, y] := true;
    if y < 4 then
        go(x, y + 1)
    else
        go(x + 1, 1);
    change(x, y);
    v[x, y] := false;
    if y < 4 then
        go(x, y + 1)
    else
        go(x + 1, 1);
end;

begin
    for i := 1 to 4 do begin
        for j := 1 to 4 do begin
            read(ch);
            if ch = 'w' then
                map[i, j] := true
            else
                map[i, j] := false;
        end;
        readln;
    end;
    min := maxlongint;
    find := false;
    go(1, 1);
    if find then
        writeln(min)
    else
        writeln('Impossible');
end.

   

posted @ 2009-01-04 12:24  jesonpeng  阅读(163)  评论(0编辑  收藏  举报