题目通道
瞎撞式的 dfs
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
while ( a>='0' && a<='9' ){ x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
int a,b;
}anse[100],dfse[100];
int ans = inf;
void dfs(int x,int y,int dep){
if ( x == 4 && y == 4 ){
if ( ans > dep ){
ans = dep;
rep(i,1,dep)
anse[i].a = dfse[i].a,
anse[i].b = dfse[i].b;
}
return ;
}
if ( x+1<=4 && e[x+1][y] == 0 ){
dfse[dep+1].a = x+1;
dfse[dep+1].b = y;
dfs(x+1,y,dep+1);
}
if ( y+1<=4 && e[x][y+1] == 0 )
{
dfse[dep+1].a = x;
dfse[dep+1].b = y+1;
dfs(x,y+1,dep+1);
}
}
int main()
{
// freopen("1.txt","r",stdin);
// srand((int)time(0));
// std::ios::sync_with_stdio(false);
// int n;
rep(i,0,4) rep(j,0,4)
scanf("%d",&e[i][j]);
anse[1].a = anse[1].b = 0;
dfs(0,0,1);
rep(i,1,ans){
printf("(%d, %d)",anse[i].a,anse[i].b);
if ( ans != i )
cout<<endl;
}
return 0;
}
迭代dfs 相对暴力
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
while ( a>='0' && a<='9' ){ x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
int a,b;
}anse[100],dfse[100];
int ans = inf;
bool vis[N][N];
bool isin ( int x,int y){
if ( 0 <= x && x <= 4 && 0 <= y && y <= 4 ) return true;
return false;
}
int nxt[][2] = {1,0,-1,0,0,1,0,-1};
bool ok = 0;
void dfs(int x,int y,int dep,int lim){
if ( dep > lim ) return ;
if ( x == 4 && y == 4 ){
if ( ans > dep ){
ok = 1;
ans = dep;
rep(i,1,dep)
anse[i].a = dfse[i].a,
anse[i].b = dfse[i].b;
}
return ;
}
rep(i,0,3){
int nx,ny;
nx = x + nxt[i][0];
ny = y + nxt[i][1];
if ( isin(nx,ny) && e[nx][ny] == 0 && vis[nx][ny] == 0){
vis[nx][ny] = 1;
dfse[dep].a = nx;
dfse[dep].b = ny;
dfs(nx,ny,dep+1,lim);
vis[nx][ny] = 0;
}
}
}
int main()
{
freopen("1.txt","r",stdin);
// srand((int)time(0));
// std::ios::sync_with_stdio(false);
// int n;
rep(i,0,4) rep(j,0,4)
scanf("%d",&e[i][j]);
anse[0].a = anse[0].b = 0;
vis[0][0] = 1;
memset(vis,0,sizeof(vis));
for (int i = 1; i <= 29; i++){
if ( ok == 0 )
dfs(0,0,1,i);
}
rep(i,0,ans-1){
printf("(%d, %d)",anse[i].a,anse[i].b);
if ( ans != i )
cout<<endl;
}
return 0;
}
迭代dfs 很暴力
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
while ( a>='0' && a<='9' ){ x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
int a,b;
}anse[100],dfse[100];
int ans = inf;
bool vis[N][N];
bool isin ( int x,int y){
if ( 0 <= x && x <= 4 && 0 <= y && y <= 4 ) return true;
return false;
}
int nxt[][2] = {1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int dep){
if ( x == 4 && y == 4 ){
if ( ans > dep ){
ans = dep;
rep(i,1,dep)
anse[i].a = dfse[i].a,
anse[i].b = dfse[i].b;
}
return ;
}
rep(i,0,3){
int nx,ny;
nx = x + nxt[i][0];
ny = y + nxt[i][1];
if ( isin(nx,ny) && e[nx][ny] == 0 && vis[nx][ny] == 0){
vis[nx][ny] = 1;
dfse[dep].a = nx;
dfse[dep].b = ny;
dfs(nx,ny,dep+1);
vis[nx][ny] = 0;
}
}
// if ( x+1<=4 && e[x+1][y] == 0 ){
// dfse[dep+1].a = x+1;
// dfse[dep+1].b = y;
// dfs(x+1,y,dep+1);
// }
// if ( y+1<=4 && e[x][y+1] == 0 )
// {
// dfse[dep+1].a = x;
// dfse[dep+1].b = y+1;
// dfs(x,y+1,dep+1);
// }
//
}
int main()
{
freopen("1.txt","r",stdin);
// srand((int)time(0));
// std::ios::sync_with_stdio(false);
// int n;
rep(i,0,4) rep(j,0,4)
scanf("%d",&e[i][j]);
anse[0].a = anse[0].b = 0;
vis[0][0] = 1;
memset(vis,0,sizeof(vis));
dfs(0,0,1);
rep(i,0,ans-1){
printf("(%d, %d)",anse[i].a,anse[i].b);
if ( ans != i )
cout<<endl;
}
return 0;
}
标准解法 bfs
不写代码,so easy