1、棋盘问题 POJ - 1321
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<cstring>
using namespace std;
void dfs(int row);
char data[8][8];
bool judge[8];
int n, k, ans, cnt;
int main(){
cin>>n >>k;
while (n!=-1 && k!=-1) {
if (n == 0) {
cout<<0<<endl;
cin >>n >>k;
continue;
}
int i;
for (i=0; i<n; i++) {
for (int j=0; j<n; j++){
cin>>data[i][j];
}
}
ans = 0;
cnt = 0;
memset(judge, true, sizeof(judge));
dfs(0);
cout<<ans <<endl;
cin>>n >>k;
}
return 0;
}
void dfs(int row) {
if (cnt == k) {
ans ++;
return;
}
if (row >= n) return;
for (int i=0; i<n; i++) {
if (judge[i] && data[row][i]=='#') {
cnt ++;
judge[i] = false;
dfs (row+1);
cnt --;
judge[i] = true;
}
}
dfs(row+1);
}
2、Dungeon Master POJ - 2251
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
char map[33][33][33];
int l,r,c,i,j,k,bx,by,bz,ans;
int ne[6][3] = {
{
0,0,1},{
0,0,-1},{
0,1,0},{
0,-1,0},{
1,0,0},{
-1,0,0}};
struct stu
{
int x,y,z,step;
};
bool go(stu st)
{
if(st.x<0 || st.z<0 || st.y<0 || st.y>=r || st.z>=c || st.x>=l || map[st.x][st.y][st.z] =='#')
return false;
return true;
}
int bfs(int x,int y,int z)
{
map[x][y][z] = '#';
int time,xx,yy,zz;
queue<stu> que;
stu st;
st.x = x;
st.y = y;
st.z = z;
st.step = 0;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
xx = st.x;
yy = st.y;
zz = st.z;
time = st.step;
for(i=0;i<6;i++)
{
st.x = xx+ne[i][0];
st.y = yy+ne[i][1];
st.z = zz+ne[i][2];
if(!go(st))
{
continue;
}
if(map[st.x][st.y][st.z]=='E')
{
return time+1;
}
st.step = time+1;
que.push(st);
map[st.x][st.y][st.z] = '#';
}
}
return 0;
}
int main()
{
int i,j,k;
while(cin>>l>>r>>c && l && r && c) {
int x0, y0, z0;
for(i=0;i<l;i++) {
for(j=0;j<r;j++) {
for(k=0;k<c;k++) {
cin>>map[i][j][k];
if(map[i][j][k]=='S') {
x0 = i;
y0 = j;
z0 = k;
}
}
}
getchar();
}
ans = bfs(x0, y0, z0);
if (ans) {
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
else {
cout<<"Trapped!"<<endl;
}
}
return 0;
}
3、Catch That Cow POJ - 3278
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,k,ans;
int book[100105];
struct val
{
int x,time;
};
int bfs(){
queue<val> que;
val va;
va.x = n;
va.time = 0;
que.push(va);
while(!que.empty()) {
va = que.front();
que.pop();
if(va.x+1==k || va.x-1==k || va.x*2==k) return va.time+1;
if (va.x+1 != k && book[va.x+1]==0) {
val temp;
temp.x = va.x + 1;
temp.time = va.time + 1;
book[temp.x] = 1;
que.push(temp);
}
if (va.x-1 != k && va.x-1>=0 && book[va.x-1]==0) {
val temp;
temp.x = va.x - 1;
temp.time = va.time + 1;
book[temp.x] = 1;
que.push(temp);
}
if (va.x * 2 <= k+5 && book[va.x*2]==0) {
val temp;
temp.x = va.x * 2;
temp.time = va.time + 1;
book[temp.x] = 1;
que.push(temp);
}
}
}
int main(){
cin>>n>>k;
book[n] = 1;
if(n!=k){
ans = bfs();
}
cout<<ans<<endl;
return 0;
}
4、POJ 3279(更新ing)
5、Find The Multiple POJ - 1426
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n;
long long ans;
bool bo = false;
void dfs(int cnt, long long k){
if(cnt>=20 || bo) return;
if (k % n == 0) {
ans = k;
bo = true;
return;
}
dfs(cnt+1, k*10);
dfs(cnt+1, k*10+1);
}
int main(){
while(cin>>n && n!=0) {
ans = 0;
bo = false;
dfs(1,1);
cout<<ans<<endl;
}
return 0;
}
6、Prime Path POJ - 3126
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int table[10004];
int book[10004];
int sta,ending;
struct val
{
int x,time;
};
void makeTable(){
for(int i=2; i<103; i++){
if (table[i] == 0) {
for(int j=i*i; j<10004; j+=i) {
if(table[j] == 0){
table[j] = i;
}
}
}
}
}
void bfs(){
val va;
va.x = sta;
va.time = 0;
queue<val> que;
que.push(va);
book[va.x] = 1;
while(!que.empty()){
va = que.front();
que.pop();
for(int i=0; i<4; i++){
for(int j=0; j<10; j++){
if(i==0 && j==0) continue;
int num ;
if (i==0){
num = va.x - va.x/1000*1000 + j*1000;
}
else if(i==1){
num = va.x - (((va.x/100) %10)*100) + j*100;
}
else if(i==2){
num = va.x - ((va.x/10)%10)*10 + j*10;
}
else if(i==3){
num = va.x - va.x%10 + j;
}
if (num == ending){
cout<<va.time+1<<endl;
return;
}
if(book[num]==0 && table[num]==0){
val temp;
temp.x = num;
temp.time = va.time+1;
que.push(temp);
book[num] = 1;
}
}
}
}
cout<<"Impossible"<<endl;
}
7、Shuffle’m Up POJ - 3087
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int main(){
int t;
cin>>t;
for(int i=1; i<=t; i++){
int n, ans=0;
string str0, str1, str;
cin>>n>>str0>>str1>>str;
bool bo = false;
for (int j=0; j<2*n-1; j++){
string temp = str;
int index0=0, index1=0;
for(int k=1; k<=2*n; k++) {
if(k%2 == 1){
temp[k-1] = str1[index1++];
}
else{
temp[k-1] = str0[index0++];
}
}
if(str == temp){
cout<<i<<" "<<ans+1<<endl;
bo = true;
break;
}
else {
ans ++;
str0 = temp.substr(0,n);
str1 = temp.substr(n,n);
}
}
if(!bo) cout<<i<<" "<<-1<<endl;
}
return 0;
}
8、POJ 3414(更新ing)
9、FZU 2150(更新ing)
10、UVA 11624(更新ing)
11、迷宫问题 POJ 3984
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int map[10][10];
int dir[4][2] = {
{
1,0},{
0,1},{
0,-1},{
-1,0}};
struct val
{
int x,y;
};
val way[7][7];
void bfs(){
val va;
va.x = 5;
va.y = 5;
queue<val> que;
que.push(va);
while(!que.empty()){
va = que.front();
que.pop();
for(int i=0; i<4; i++){
int x = va.x + dir[i][0];
int y = va.y + dir[i][1];
if (map[x][y] == 0){
val temp;
temp.x = x;
temp.y = y;
way[x][y] = va;
map[x][y] = 1;
if (x==1 && y==1){
return;
}
que.push(temp);
}
}
}
}
int main(){
memset(map, 1, sizeof(map));
for(int i=1; i<=5; i++){
for(int j=1; j<=5; j++){
cin>>map[i][j];
}
}
bfs();
int x = 1, y = 1;
while(true){
cout<<"("<<x-1<<", "<<y-1<<")"<<endl;
if(x==5 && y==5) break;
int temp = x;
x = way[x][y].x;
y = way[temp][y].y;
}
return 0;
}
12、HDU 1241(更新ing)
13、HDU 1495(更新ing)
14、HDU 2612(更新ing)