启发式搜索,全WA代码求调(样例已过)

P1746 离开中山路

Gumbo @ 2022-06-17 21:07:29

RT

#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 1005
#define abs(x) (x > 0 ? x : -(x))
using namespace std;
short tx, ty;
short dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
struct stable
{
   short x, y;
   short dis;
   short g;
   bool operator<(const stable &other) const
   {
      return g > other.g;
   }
   stable(short a, short b, short d) : x(a), y(b), dis(d)
   {
      g = dis + abs(tx - a) + abs(ty - b);
   }
   stable(void){}
};
short mp[MAXN][MAXN];
short frm[MAXN][MAXN];
inline short read(void)
{
   short ans = 0;
   char us = getchar();
   while (us < '0' || us > '9')
   {
      us = getchar();
   }
   while (us >= '0' && us <= '9')
   {
      ans = (ans << 1) + (ans << 3) + (us ^ 48);
      us = getchar();
   }
   return ans;
}
int main()
{
   priority_queue<stable> q;
   short n;
   n = read();
   short i, j;
   for (i = 0; i <= n + 1; ++i)
   {
      mp[i][0] = mp[i][n + 1] = mp[0][i] = mp[n + 1][i] = 1;
   }
   for (i = 1; i <= n; ++i)
   {
      for (j = 1; j <= n; ++j)
      {
         mp[i][j] = getchar() ^ 48;
      }
      getchar();
   }
   i = read();
   j = read();
   tx = read();
   ty = read();
   q.push(stable(i, j, 0));
   stable u;
   while (!q.empty())
   {
      u = q.top();
      q.pop();
      if (u.x == tx && u.y == ty)
      {
         printf("%d", u.dis);
         return 0;
      }
      for (i = 0; i < 4; ++i)
      {
         q.push(stable(u.x+dx[i], u.y+dy[i],u.dis+1));
      }
   }
   return 0;
}
\color{grey}{\texttt{启发估价函数:曼哈顿距离}}

by llycdasanbing @ 2022-07-29 15:25:48

我这里没看明白你的程序,但是可以给你一组大数据:

输入:
10
0101010101
0000000000
1111101010
0000000000
1110111111
1110000111
1010101010
1000000000
1000111111
1000000000
1 1
10 10
正确输出:
22
你的输出:
18

|