5.1题解---棋盘+Cow Travelling S(搜索)

1、棋盘
解题思路:
(1)用dfs,最优性剪枝:每次进入dfs更新从1,1到x,y的最小花费,如果大于已经存储的值,那么就可以停止了。用一个变量来记录是否可以使用魔法,使用魔法之后将颜色变为与父节点一样。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n,m;
int dist[N][N],g[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int x,int y,int cost,int col,int flag)
{
    if(dist[x][y] <= cost) return;
    dist[x][y] = cost; // 这一行是关键
    
    for (int i = 0; i < 4; i ++ )
    {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 1 || nx > n || ny < 1 || ny > n) continue;
        
        if(col == g[nx][ny])
        {
            dfs(nx,ny,cost,col,1);
        }
        else
        {
            if(g[nx][ny] == -1)
            {
                if(flag) dfs(nx,ny,cost+2,col,0);
            }
            else dfs(nx,ny,cost+1,g[nx][ny],1);
        }
    }
}
int main()
{
    memset(dist,0x3f,sizeof dist);
    memset(g,-1,sizeof g);
    cin >> n >> m;
    while (m -- )
    {
        int x,y,c;
        cin >> x >> y >> c;
        g[x][y] = c;
    }
    dfs(1,1,0,g[1][1],1);
    if(dist[n][n] == 0x3f3f3f3f) cout << -1 << endl;
    else cout << dist[n][n] << endl;
    
    return 0;
    
}

(2) bfs来写,用一个优先队列来维护,类似于跑堆优化版的迪杰斯特拉

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010;
int g[N][N];
int dist[N][N][2];
bool st[N][N][2];
int n,m;
struct Node
{
    int x,y,c,d;
    Node(int _x,int _y,int _c,int _d)
    {
        x = _x;
        y = _y;
        c = _c;
        d = _d;
    }
    bool operator<(const Node &n) const {
        return n.d < this->d;
    }
};
typedef pair<int, Node> PII;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool cmp(PII &p1,PII &p2)
{
    return p1.first < p2.first;
}
int dijkstra()
{
    memset(dist,0x3f,sizeof dist);
    dist[1][1][g[1][1]] = 0;
    priority_queue<Node> q;
    q.push(Node(1,1,g[1][1],0));
    while(q.size())
    {
        auto t = q.top();
        q.pop();
        int distance = t.d;
        int sx = t.x,sy = t.y,c = t.c;
        if(st[sx][sy][c]) continue; //关键的剪枝
        st[sx][sy][c] = true;
        
        for (int i = 0; i < 4; i ++ )
        {
            int nx = sx+dx[i],ny = sy + dy[i];
            if(nx < 1 || nx > n || ny < 1 || ny > n) continue;
            
            int curcol = g[nx][ny];
            if(curcol != -1)
            {
                // 有颜色的格子
                if(curcol == c)
                {
                    if(dist[nx][ny][c] > distance)
                    {
                        dist[nx][ny][c] = distance;
                        q.push(Node(nx,ny,c,distance));
                    }
                }
                else
                {
                    if(dist[nx][ny][curcol] > distance + 1)
                    {
                        dist[nx][ny][curcol] = distance + 1;
                        q.push(Node(nx,ny,curcol,distance + 1));
                    }
                }
            }
            else
            {
                if(g[sx][sy]!=-1)
                {
                    // 不能从没有颜色的格子更新过来,c是变换之后的颜色,不能信
                    // 将这个格子更新成为与g[sx][sy]一样的颜色
                    int sc = g[sx][sy];
                    if(dist[nx][ny][sc] > distance + 2)
                    {
                        dist[nx][ny][sc] = distance + 2;
                        q.push(Node(nx,ny,sc,distance + 2));
                    }
                }
            }
        }
    }
    if(g[n][n] == -1)
    {
        return min(dist[n][n][0],dist[n][n][1]);
    }
    else
    {
        return dist[n][n][g[n][n]];
    }
    return 0;
}
int main()
{
    memset(g,-1,sizeof g);
    cin >> n >> m;
    while (m -- )
    {
        int x,y,c;
        cin >> x >> y >> c;
        g[x][y] = c;
    }
    int ans = dijkstra();
    if(ans == 0x3f3f3f3f) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}

2、P1535 [USACO08MAR]Cow Travelling S
题意概括:求出在给定时间内,从一个点到另一个点的所有方案数
解题思路:用dfs,可行性剪枝:如果当前点到终点的最短时间都比剩下的时间长,那么这个点就可以不用遍历了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
char g[N][N];
int n,m,t;
int sx,sy,ex,ey;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int ans;
bool st[N][N];
void dfs(int x,int y,int rest)
{
    if(x == ex && y == ey && rest == 0) ans++;
    if(rest <= 0) return; // 这一个也很关键
    //cout << x << " " << y << endl;
    for (int i = 0; i < 4; i ++ )
    {
        int nx = x + dx[i],ny = y + dy[i];
        if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
        if(abs(nx-ex) + abs(ny-ey) > rest || g[nx][ny] == '*') continue;
        dfs(nx,ny,rest-1);
    }
}
int main()
{
    cin >> n >> m >> t;
    for (int i = 1; i <= n; i ++ ) cin >> g[i] + 1;
    cin >> sx >> sy >> ex >> ey;
    dfs(sx,sy,t); // 坐标和剩余时间
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值