# HDOJ水题集合6：杂题

#### 1001

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 33 Accepted Submission(s) : 16
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description

Input

Output

Sample Input
3
2
Sample Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2
2 1

#include<iostream>
using namespace std;
int n, c[20];
void dfs(int cur){
if(cur == n){
for(int i = 0; i < n; i++)cout<<c[i]<<" ";
cout<<"\n";
}else for(int i = 1; i <= n; i++){
int ok = 1;
for(int j = 0; j < cur; j++)
if(c[j]==i)ok = 0;
if(ok){
c[cur] = i;
dfs(cur+1);
}
}
}
int main(){
while(cin>>n)dfs(0);
return 0;
}


#### 1002

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 50 Accepted Submission(s) : 18
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description

Input

‘X’：不能进入的墙
‘S’：起点
‘D’：门
‘.’：可以行走的地方

Output

Sample Input
4 4 5
S.X.
…X.
…XD

3 4 5
S.X.
…X.
…D
0 0 0
Sample Output
NO
YES

• 不能广搜的原因：考虑1->2,1->3后vis[2,3]==1，所以2->3不能走了
#include<bits/stdc++.h>
using namespace std;

char a[20][20];
int vis[20][20];
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};

int n, m, T;
int sx, sy, ex, ey, ok;
void dfs(int x, int y, int step){
if(x==ex&&y==ey&&step==T){ok=1; return ;}
if(step>T || ok==1)return ;
if(abs(T-step-abs(x-ex)-abs(y-ey))%2==1)return ;
for(int i = 0; i < 4; i++){
int nx = x+dx[i], ny = y+dy[i];
if(nx>=1&&nx<=n &&ny>=1&&ny<=m &&!vis[nx][ny]&&a[nx][ny]!='X'){
vis[nx][ny] = 1;
dfs(nx,ny,step+1);
vis[nx][ny] = 0;
}
}
}

int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
while(cin>>n>>m>>T &&n&&m&&T){
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>a[i][j];
if(a[i][j]=='S'){sx=i;sy=j;}
if(a[i][j]=='D'){ex=i;ey=j;}
}
}
ok = 0;
vis[sx][sy] = 1;
dfs(sx,sy,0);
if(ok)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}



10-12 6337
03-10 3310
05-31 508
03-12 4530
04-06 584
10-04 2560
07-11 143
04-20 97
11-05 372
05-16 786
08-01 895
06-12 1055
12-10 3224