UVa211 - The Domino Effect(DFS)

简介: UVa211 - The Domino Effect(DFS)
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.FileReader;
importjava.io.PrintWriter;
importjava.io.OutputStreamWriter;
importjava.io.StreamTokenizer;
importjava.io.IOException;
classMain{
publicstaticfinalbooleanDEBUG=false;
publicStreamTokenizertokenizer;
publicPrintWritercout;
BufferedReadercin;
publicfinalintROW=7;
publicfinalintCOL=8;
publicfinalintSIZE=30;
publicint[][] grid=newint[ROW][COL];
publicboolean[] vis=newboolean[SIZE];
publicint[][] flag=newint[ROW][COL];
publicint[][] map=newint[ROW][ROW];
publicint[][] dir= {{0, 1}, {1, 0}};
publicintcnt;
publicvoidinit() throwsIOException    {       
if (DEBUG) {
cin=newBufferedReader(newFileReader("d:\\OJ\\uva_in.txt"));
        } else {
cin=newBufferedReader(newInputStreamReader(System.in));
        }
tokenizer=newStreamTokenizer(cin);
cout=newPrintWriter(newOutputStreamWriter(System.out)); 
intc=1;
for (inti=0; i<ROW; i++) {
for (intj=i; j<ROW; j++) {
map[i][j] =map[j][i] =c++;
            }
        }
    }
publicintnext() throwsIOException    {
tokenizer.nextToken();
if (tokenizer.ttype==StreamTokenizer.TT_NUMBER) {
return (int)tokenizer.nval;
        } elsereturn-1;
    }
publicbooleaninput() throwsIOException    {
for (inti=0; i<ROW; i++) {
for (intj=0; j<COL; j++) {
intc=next();
if (c==-1) returnfalse;
grid[i][j] =c;
            }
        }
returntrue;
    }
publicvoiddfs(intx, inty)
    {
if (y>=COL) {
x++;
y=0;
if (x>=ROW) {
cnt++;
print();
return;
            }
        }
if (flag[x][y] !=0) dfs(x, y+1);
else {
for (inti=0; i<2; i++) {
intdx=x+dir[i][0];
intdy=y+dir[i][1];
if (dx>=ROW||dy>=COL||flag[dx][dy] !=0) continue;
intc=map[grid[x][y]][grid[dx][dy]];
if (vis[c]) continue;
vis[c] =true;
flag[x][y] =flag[dx][dy] =c;
dfs(x, y+1);
vis[c] =false;
flag[x][y] =flag[dx][dy] =0;
            }
        }
    }
publicvoidprint()
    {
for (inti=0; i<ROW; i++) {
for (intj=0; j<COL; j++) {
cout.printf("%4d", flag[i][j]);
            }
cout.println();
        }
cout.println();
    }
publicvoidsolve(intcas)
    {
if (cas!=1) {
cout.println();
cout.println();
cout.println();
        }
cout.println("Layout #"+cas+":");
cout.println();
for (inti=0; i<ROW; i++) {
for (intj=0; j<COL; j++) {
cout.printf("%4d", grid[i][j]);
            }
cout.println();
        }
cnt=0;
cout.println();
cout.println("Maps resulting from layout #"+cas+" are:");
cout.println();
dfs(0, 0);
cout.println("There are "+cnt+" solution(s) for layout #"+cas+".");
cout.flush();
    }
publicstaticvoidmain(String[] args) throwsIOException    {
Mainsolver=newMain();
solver.init();
intcas=1;
while (solver.input()) {
solver.solve(cas);
cas++;
        }
    }
}
目录
相关文章
|
6月前
|
PHP
tp5 宝塔open_basedir restriction in effect 错误; IIS open_basedir restriction in effect
一个旧的微信项目在换部署环境后出错,客户使用的是iis8。尽管查阅了官方手册和大量建议修改php.ini的百度解决方案,问题仍未解决。最终,通过关闭防跨站攻击设置,并重启php和iis,成功解决了网站无法访问的问题。
35 2
|
7月前
|
流计算
POJ 3620--Avoid The Lakes【DFS】
POJ 3620--Avoid The Lakes【DFS】
36 0
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
65 0
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
|
机器人
HDU-1035,Robot Motion(DFS)
HDU-1035,Robot Motion(DFS)
|
机器学习/深度学习
CodeForces 6B-President‘s Office(简单的DFS)
CodeForces 6B-President‘s Office(简单的DFS)
|
机器学习/深度学习
HDOJ 2212 DFS
HDOJ 2212 DFS
114 0
HDOJ/HDU 1241 Oil Deposits(经典DFS)
HDOJ/HDU 1241 Oil Deposits(经典DFS)
91 0