hdu5335 搜索+优化

题目意思:

给定一个n*m的图,图上的每个点上的值为0或1,让你找到一条路,所经过的01序列最小。


思路:若开始为0,则广搜找到最远的0(离终点最近,即x+y最大)。然后开始用优先级队列搜索。

另外一种就是找到最远点后模拟搜索或者说斜推找到最小的(这种方法比较科学,因为和dir数组没有关系)。


ps:刚开始直接用广搜超时了,然后知道可能要找到最远的0,但是想复杂了。比赛时没做出来,有点遗憾。。。。dir数组必须先向下在向右,否则WA,不知为什么,难道数据弱?(本来想的是记录所有最大x+y,搜索每一个点,将所有结果比较,找到最小的,没想到……………………………………)


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <bitset>

using namespace std;

#define MAXN 1000005
char arr[1005][1005];
int visit[1005][1005];
int m , n;
string res , str;
string minn;
int dir[4][2] = {1 , 0 ,  0 , 1 , 0 , -1 , -1 , 0 };
int maxx , maxy;

struct point
{
    int x,y;
    string num;
    friend bool operator< (point n1, point n2)
    {
        return n1.num > n2.num;
    }
};

void BFS2()
{
    queue<point>Q;
    point cur , temp;
    cur.x = maxx ;
    cur.y = maxy ;
    Q.push(cur);
    while(!Q.empty())
    {
        cur = Q.front();
        Q.pop();
        for(int i = 0 ; i < 4 ; i ++)
        {
            temp.x = cur.x + dir[i][0];
            temp.y = cur.y + dir[i][1];
            if(temp.x >=0 && temp.x < m && temp.y >= 0 && temp.y < n && !visit[temp.x][temp.y])
            {
                visit[temp.x][temp.y] = 1;
                if(arr[temp.x][temp.y] == '0')
                    Q.push(temp);
                if(temp.x + temp.y > maxx + maxy)
                    maxx = temp.x , maxy = temp.y;
            }
        }
    }
}

void BFS(int x ,int y)
{
    priority_queue <point>Q;
    point cur , temp;
    cur.x = x;
    cur.y = y;
    cur.num = arr[x][y];
    Q.push(cur);
    while(!Q.empty())
    {
        cur = Q.top();
        Q.pop();
        if(cur.x == m - 1 && cur.y == n - 1)
        {
            res = cur.num;
            break;
        }
        for(int i= 0 ; i < 2 ; i ++ )
        {
            temp.x = cur.x + dir[i][0];
            temp.y = cur.y + dir[i][1];
            if(temp.x >=0 && temp.x < m && temp.y >= 0 && temp.y < n && !visit[temp.x][temp.y])
            {
                temp.num = cur.num  + (arr[temp.x][temp.y]);
                visit[temp.x][temp.y] = 1;
                Q.push(temp);
            }
        }
    }
}

int main()
{
    int t ;
    scanf("%d" , &t);
    while(t --)
    {
        scanf("%d %d" , &m , &n);
        for(int i = 0 ; i < m ; i++) scanf("%s" , &arr[i]);
        res = "";
        str = "";
        memset(visit , 0 ,sizeof(visit));
        maxx = maxy = 0;
        if(arr[0][0] == '0') BFS2();
        if(arr[maxx][maxy] == '0')
            cout << "0" << endl;
        else
        {
            BFS(maxx , maxy);
            cout << res << endl;
        }
    }
}

点击打开链接斜推代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1010;
struct node
{
    int x, y;
};
char maps[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {0, 1 , 1, 0 , -1,0, 0,-1};
int x, y, n, m;
bool Ok (int x, int y)
{
    if (x<0 || y<0 || x>=n || y>=m)
        return false;
    return true;
}
void bfs ()
{
    node p, q;
    p.x = x, p.y = y;
    queue <node> Q;
    Q.push (p);
    while (!Q.empty())
    {
        p = Q.front();
        Q.pop();
        for (int i=0; i<4; i++)
        {
            q.x = p.x + dir[i][0];
            q.y = p.y + dir[i][1];
            if (Ok(q.x, q.y) && !vis[q.x][q.y])
            {
                vis[q.x][q.y] = true;
                if (maps[q.x][q.y] == '0')
                    Q.push (q);
                if (x + y < q.x + q.y)
                    x = q.x, y = q.y;
            }
        }
    }
}
int main ()
{
    int t;
    scanf ("%d", &t);
    while (t --)
    {
        memset (vis, false, sizeof(vis));
        vis[0][0] = true;
        scanf ("%d %d", &n, &m);
        for (int i=0; i<n; i++)
            scanf ("%s", maps[i]);
        x = y = 0;
        if (maps[x][y] == '0')
            bfs ();
        if (maps[x][y] == '0')
            putchar('0');
        else
        {
            bool nowflag = false;
            putchar ('1');
            for (int i=x+y; i<n+m-2; i++)
            {
                bool flag = false;
                for (x=0; x<=i; x++)
                {
                    y = i - x;
                    if (!Ok(x, y) || !vis[x][y])
                        continue;
                    if (nowflag && maps[x][y]=='1')
                        continue;
                    for (int j=0; j<2; j++)
                    {
                        int a = x + dir[j][0];
                        int b = y + dir[j][1];
                        if (!Ok(a, b))
                            continue;
                        vis[a][b] = true;
                        if (maps[a][b] == '0')
                            flag = true;
                     }
                }
                nowflag = flag;
                putchar (flag?'0':'1');
            }
        }
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值