SDNUOJ——1025.馬踏飛燕(DFS),1027馬踏飛燕(續)(BFS)......其實,有bug。。。

問題蟲洞: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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章