牛客网——A逃脱(可以AC了)

我使用了递归的dfs没有通过,“我的提交里”看到测试用例只有一个,结果是输出超时,估计是递归法使得复杂度陡增,这是题目:

虽然0分,我还是保存一下代码供以后参考:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;

bool flag;
bool testunit[30][30];
int n, m;
char map[30][30];
int ex, ey;
int sx, sy;
int firex, firey;
int minn;
int dr[4] = { -1, 0, 1, 0 };
int dc[4] = { 0, 1, 0, - 1 };

float distance(int x1, int y1, int x2, int y2)
{
	return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
	

void dfs(int x,int y,int cnt,int flag1,int flag2)
{
	if (x == ex && y == ey) 
	{ 
		flag = true;
		if (cnt < minn)minn = cnt;
		return; 
	}

	for (int i = 0; i < 4; i++)
	{
		if (i == flag1 || i == flag2)continue;
		if (x + dr[i] >= 0 && x + dr[i] < n&&y + dc[i] >= 0 && y + dc[i] < m&&map[x + dr[i]][y + dc[i]] != '#'&&!testunit[x + dr[i]][y + dc[i]])
		{
			testunit[x + dr[i]][y + dc[i]] = true;
			dfs(x + dr[i], y + dc[i], cnt + 1, flag1, flag2);
			testunit[x + dr[i]][y + dc[i]] = false;		
		}
	}
	return;
}

int main(void)
{
	memset(testunit, false, sizeof(testunit));
	int N;
	cin >> N;
	for (int k = 0; k < N; k++)
	{
		flag = false;
		minn = 100000;
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
			for (int j = 0; j < m; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == 'E') { ex = i; ey = j; }
				if (map[i][j] == 'S') { sx = i; sy = j; }
				if (map[i][j] == '*') { firex = i; firey = j; }
			}
		if (distance(ex, ey, sx, sy) < (firex, firey, ex, ey))
		{
			int flag1, flag2;
			if (ex >= sx)flag1 = 3;
			else flag1 = 1;
			if (ey >= sy)flag2 = 0;
			else flag2 = 2;
			dfs(sx, sy, 0,flag1,flag2);
		}
		testunit[sx][sy] = true;
		if (flag == false)cout << "T_T" << endl;
		else cout << minn << endl;
	}

	system("pause");
	return 0;
}

于是又试了BFS,代码如下:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
  
struct node
{
    int x, y;
    int step;
}S,E,fire,Node;
  
bool flag;
bool testunit[35][35];
int n, m;
char map[35][35];
int dr[4] = { -1, 0, 1, 0 };
int dc[4] = { 0, 1, 0, -1 };
  
float distance(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
  
bool test(int x, int y)
{
    if (x < 0 && x >= n && y < 0 && y >= m)return false;
    if (testunit[x][y] == true)return false;
    if (map[x][y] == '#')return false;
    return true;
}
  
int bfs()
{
    queue<node> Q;
    Q.push(S);
    while (!Q.empty())
    {
        node top = Q.front();
        Q.pop();
        if (top.x == E.x && top.y == E.y)return top.step;
  
        for (int i = 0; i < 4; i++)
        {
            int X = top.x + dr[i];
            int Y = top.y + dc[i];
            if (test(X, Y))
            {
                Node.x = X;
                Node.y = Y;
                Node.step = top.step + 1;
                Q.push(Node);
                testunit[X][Y] = true;
            }
        }
    }
  
    return -1;
}
  
int main(void)
{
    memset(testunit, false, sizeof(testunit));
    memset(map, '#', sizeof(map));
    int N;
    cin >> N;
    for (int k = 0; k < N; k++)
    {
        flag = false;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                char temp;
                cin >> temp;
                map[i][j] = temp;
                if (map[i][j] == 'E') { E.x = i; E.y = j; }
                if (map[i][j] == 'S') { S.x = i; S.y = j; }
                if (map[i][j] == '*') { fire.x = i; fire.y = j; }
            }
        int result = -1;
        if (distance(E.x, E.y, S.x, S.y) < distance(fire.x, fire.y, E.x, E.y))
        {
            result = bfs();
        }
        testunit[S.x][S.y] = true;
        if (result == -1)cout << "T_T" << endl;
        else cout << result << endl;
    }
  
    system("pause");
    return 0;
}

 结果显示段错误,应该是循环多次导致栈溢出,我于是看了一个AC的代码,他用了回溯法,我把BFS那边的注释写了下,如下所示,是可以AC的:

#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
int n, m;
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
char mp[31][31];
int vis[31][31], f[31][31];
struct node
{
    int x;
    int y;
    int step;
};
int sx, sy, fx, fy;
queue<node>q;
void bfs(int x, int y)
{
    queue<node>q;
    node a, b, temp1, temp2;
    a.x = x;
    a.y = y;
    a.step = 0;
    q.push(a);
    while (!q.empty())
    {
        a = q.front();
        q.pop();
        //满足条件时
        if (mp[a.x][a.y] == 'E')
        {
            cout << a.step << endl;
            return;

        }
        //这一步是用来简化复杂度的,是回溯算法,
        //满足条件的话说明当前循环以前已经运行过了,就跳过当前这一循环。
        if (f[a.x][a.y] == a.step) continue;
        for (int i = 0; i < 4; i++)
        {
            //初始化b,其意义不是把a拷贝给b
            b = a;
            //遍历下一格
            b.x = a.x + dir[i][0];
            b.y = a.y + dir[i][1];
            //这一步好像有点累赘,但是可以看出是用来下面判断是否满足遍历的条件的
            int xx = b.x;
            int yy = b.y;
            //当前加一步
            b.step += 1;
            //遍历下一格子
            if (mp[xx][yy] != '#' && b.step <= f[xx][yy] && !vis[xx][yy] && xx > 0 && xx <= n && yy > 0 && yy <= m)
            {
                q.push(b);
                vis[xx][yy] = 1;
            }
        }
    }
    //不满足条件时
    cout << "T_T" << endl;
    return;
}

int main()
{
    int t;
    cin >> t;
    while (t)
    {

        while (!q.empty()) q.pop();
        memset(vis, 0, sizeof(vis));
        cin >> n >> m;

        for (int i = 1; i <= n; i++)
        {

            for (int j = 1; j <= m; j++)
            {
                cin >> mp[i][j];
                if (mp[i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                }
                if (mp[i][j] == '*')
                {
                    fx = i;
                    fy = j;
                }
            }
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                f[i][j] = max(abs(fx - i), abs(fy - j));
            }
        vis[sx][sy] = 1;
        bfs(sx, sy);

        t = t - 1;
    }
}
发布了162 篇原创文章 · 获赞 38 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41938259/article/details/104445228