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++;
        }
    }
}
目录
相关文章
|
7月前
|
流计算
POJ 3620--Avoid The Lakes【DFS】
POJ 3620--Avoid The Lakes【DFS】
36 0
UVa11958 - Coming Home
UVa11958 - Coming Home
51 0
|
分布式计算 资源调度 Hadoop
Hadoop常见错误及解决方案、Permission denied: user=dr.who, access=WRITE, inode=“/“:summer:supergroup:drwxr-xr-x
Hadoop常见错误及解决方案、Permission denied: user=dr.who, access=WRITE, inode=“/“:summer:supergroup:drwxr-xr-x
Hadoop常见错误及解决方案、Permission denied: user=dr.who, access=WRITE, inode=“/“:summer:supergroup:drwxr-xr-x
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
|
机器学习/深度学习
CodeForces 6B-President‘s Office(简单的DFS)
CodeForces 6B-President‘s Office(简单的DFS)
|
机器学习/深度学习
HDOJ 2212 DFS
HDOJ 2212 DFS
113 0
|
Java
HDOJ1175连连看 DFS
HDOJ1175连连看 DFS
115 0
HDOJ(HDU) 1976 Software Version(简单判断)
HDOJ(HDU) 1976 Software Version(简单判断)
106 0
|
负载均衡 网络性能优化 网络架构
网络基础CCNP篇|PBR
分等级服务
1730 0