问题虫洞:1025.马踏飞燕
思维光年:
DFS爆搜
#include<stdio.h>
int n,m,p,q;
int a[105][105],book[105][105];
int flag = 0;
void DFS(int x,int y,int step)
{
int next[8][2] = {{-1,-2}, {1, -2}, {-2,-1}, {2,-1}, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}};
int tx,ty,k;
if((x == p&&y == q))
{
flag = 1;
return;
}
if(step == 4)
return;
for(k = 0; k <8; k++)
{
tx = x + next[k][0];
ty = y + next[k][1];
if(tx<1||tx>100||ty<1||ty>100)
continue;
if(a[tx][ty]==0&& book[tx][ty]==0)
{
book[tx][ty] = 1;
DFS(tx,ty,step+1);
book[tx][ty] = 0;
}
}
return;
}
int main()
{
int i,j,startx,starty;
scanf("%d %d %d %d",&startx,&starty,&p,&q);
book[startx][starty] = 1;
DFS(startx,starty,0);
if(flag)
puts("Y");
else
puts("N");
return 0;
}
问题虫洞:1027.马踏飞燕(续)
思维光年:
数据太大dfs爆搜搜爆了。。。
用bfs
ACcode:
//#include<bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include<algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
#define MAXN 1000007
#define INF 0x3f3f3f3f//将近ll类型最大数的一半,而且乘2不会爆ll
int p, q, flag=0;
struct node
{
int x, y, pre;
} g[5000005];
//queue<node>q;
bool is[2005][2005];
int f[][2] = {{-1,-2}, {1, -2}, {-2,-1}, {2,-1}, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}};
void bfs(int x, int y)
{
int head=0, tail=0;
g[tail].x = x;
g[tail].y = y;
g[tail].pre = 0;
is[x][y] = 1;
tail++;
node ans;
while(head < tail)///队列非空
{
if(g[head].x == p && g[head].y == q && g[head].pre<=200)///终止条件
{
flag = 1;
return;
}
else if(g[head].pre > 200)
{
flag=0;
return;
}
for(int i=0; i<8; ++i)///上下左右遍历该点的周围点
{
ans.x = g[head].x + f[i][0];
ans.y = g[head].y + f[i][1];
if(ans.x>0 && ans.x<=2000 && ans.y >0 && ans.y <=2000)
{
if(is[ans.x][ans.y]==0)
{
ans.pre = g[head].pre+1;
is[ans.x][ans.y] = 1;
g[tail++] = ans;
}
}
}
head++;///该点出队
}
}
int main()
{
int x, y;
memset(is, 0, sizeof(is));
scanf("%d %d %d %d", &x, &y, &p, &q);
bfs(x, y);
if(flag) puts("Y");
else puts("N");
return 0;
}
其实,这两道题是有bug数据的,,,,,比如,以下几行代码就可以飘过~~~~
1025:
#include<cstdio>
#include<iostream>
using namespace std ;
int main()
{
int a, b, c, d;
scanf("%d %d",&a,&b);
scanf("%d %d",&c,&d);
if(c>a-8&&c<a+8&&d>b-8&&d<b+8&&c-d<=4) printf("Y\n");
else printf("N\n");
return 0;
}
1027:
#include<cstdio>
#include<iostream>
using namespace std ;
int main()
{
int a, b, c, d;
scanf("%d %d",&a,&b);
scanf("%d %d",&c,&d);
if(c>a-400&&c<a+400&&d>b-400&&d<b+400&&c-d<=4) printf("Y\n");
else printf("N\n");
return 0;
}