HDU - 3912 Turn Right

Turn Right

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1352 Accepted Submission(s): 462

Problem Description
This summer, ELT and his classmates went to Beijing for a training of coding. ELT have never been to Beijing before, so at the weekend, he together with some friends went to the National Museum, it’s free for students!

The National Museum consists of many parts. One part of it is an exhibition of Ancient China From Xia Dynasty to Qing Dynasty, it needs a big room to show all the things. What’s more, there exist many walls to hang pictures. The boundary of this room is walls except the entrance and exit.

With walls, an entrance and an exit, this room can be regarded as a maze. To make it simple, this room is a R*C grid, wall is constructed at some edges of grid. The entrance is always at the first row, and the exit is always at the last row, just like the picture below.

这里写图片描述

ELT can’t remember his direction in maze, but he is a clever boy. He knew an algorithm called “Always Turn Right”, it’s procedure is as follows: at any grid of this room, if we can turn right(no wall at right side), then we must turn right; if we can’t turn right but can go straight forward, then we must go forward; if we can’t go forward but can turn left, then we must turn left; if we can’t even turn left, we just turn backward. In the picture above, if we use this algorithm, we’ll visit these grids in order: Entrance –> (0, 1) –> (0, 0) –> (0, 1) –> (0, 2) –> (1, 2) –> (1, 1) –> (1, 0) –> (2, 0) –> (1, 0) –> (1, 1) –> (2, 1) –> (2, 2) –> Exit. Very easy, doesn’t it?

ELT uses “Always Turn Right” algorithm to visit this room from entrance to exit, and then from exit to entrance. He wants to know whether he walked all grids in the room. Now ELT is dizzy because the maze is too big, can you help him?

Input
First line is an integer T, means T test cases. In each test case, the first line has four numbers: R, C, Ent_Column, Exit_Column. Ent_Column is the column number of entrance; Exit_Column is the column number of exit.
Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1 represents a wall, 0 represents no wall.
We guarantee that there exists a path from entrance to exit.
2 <= R, C <= 500
0 <= Ent_Column, Exit_Column < C

Output
If ELT can walk all grids in the room, print one line “YES”, otherwise, print one line “NO”.

Sample Input
1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 0 0 0
1 0 0

Sample Output
YES

Source
2011 Multi-University Training Contest 8 - Host by HUST

Recommend
lcy | We have carefully selected several similar problems for you: 3914 3913 3911 3910 3916

墙给的数组比较难懂
使劲看吧
就是让从起点到终点在从终点到起点 看是否跑完全部格子
有可坑点就是到了出口那个格子也不算出去只有到了那个格子并且拐出去才算出去
eg
1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 1 1 0
1 0 0

YES

#include<stdio.h>
#include<algorithm>
#include<string.h>


using namespace std;

int r,c,ec,exc;
int vis[600][600];
int count1[600][600];

int wall[1010][1010];
int dir[4][2]={0,-1,1,0,0,1,-1,0};

int check(int sx,int sy,int i)
{   

    int ex = sx+dir[i][0];
    int ey = sy+dir[i][1];
    if(ex<0||ey<0||ex>=r||ey>=c)
    {
        return 0;
//      printf("!!1\n");
    }
    if(vis[ex][ey] == 1)
    {
        return 0;
//      printf("!!2\n");
    }
    switch(i)
    {
        case 0:
            if(wall[2*sx+1][sy-1] == 1)
            {
//              printf("%d %d %d %d %d",sx,sy,2*sx+1,sy-1,wall[2*sx+1][sy-1]);
//              printf("!!3\n");
                return 0;
            }
            break;
        case 1:
            if(wall[2*sx+2][sy] == 1)
            {
//              printf("!!4\n");
                return 0;
            }
            break;
        case 2:
            if(wall[2*sx+1][sy] == 1)
            {
//              printf("!!5\n");
                return 0;
            }
            break;
        case 3:
            if(wall[2*sx][sy] == 1)
            {
//              printf("!!6\n");
                return 0;
            }
            break;

    }
    return 1;
}
int FF = 0;
int dfs(int sx,int sy,int ex,int ey,int pre,int out)
{
    if(FF == 1)
    {
        return 0;
    }
    vis[sx][sy] = 1;
    count1[sx][sy] = 1;
    int nx = sx;
    int ny = sy;
//  printf(" %d %d\n",nx,ny);


        int four = 3;
        int i = pre;
        while(four--)
        {
            i %= 4;
            if(nx == ex&&ny == ey&&i == out)
            {
                FF = 1;
                return 0;
            }
            int nextx = sx+dir[i][0];
            int nexty = sy+dir[i][1];
            if(check(nx,ny,i))
            {
//              printf("%d %d %d\n",nextx,nexty,i);
                vis[nextx][nexty] = 1;
                count1[sx][sy] = 1;
                dfs(nextx,nexty,ex,ey,(i+3)%4,out);
            }
            i++;
        }
    return 0;
}



int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        memset(wall,0,sizeof(wall));
        memset(count1,0,sizeof(count1));

        scanf("%d%d%d%d",&r,&c,&ec,&exc);
        for(int i = 1;i < 2*r;i++)
        {
            if(!(i%2))//2 4
            {
                for(int j = 0;j < c;j++)
                {
                    scanf("%d",&wall[i][j]);
                }
            }
            else//1 3
            {
                for(int jj = 0;jj < c-1;jj++)
                {
                    scanf("%d",&wall[i][jj]);
                }
            }
        }
//      for(int i = 0;i < 2*r-1;i++)
//      {
//          if(i%2)//1 3
//          {
//              for(int j = 0;j < c;j++)
//              {
//                  printf("%d ",wall[i][j]);
//              }
//          }
//          else//0 2
//          {
//              for(int jj = 0;jj < c-1;jj++)
//              {
//                  printf("%d ",wall[i][jj]);
//              }
//          }
//          printf("\n"); 
//      }
//      
//      for(int i = 0;i < 7;i++)
//      {
//          for(int j = 0;j<4;j++)
//          {
//              printf("%d ",wall[i][j]);
//          }
//          printf("\n");
//      }
        FF = 0;
        dfs(0,ec,r-1,exc,0,1);

        int sum = 0;


        memset(vis,0,sizeof(vis));
        FF = 0;
        dfs(r-1,exc,0,ec,2,3);

        for(int i = 0;i < r;i++)
        {
            for(int j = 0;j<c;j++)
            {
                sum+=count1[i][j];
            }
        }
//      printf("%d\n",sum);
        if(sum == r*c)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

}
HDU-3480 是一个典型的动态规划问题,其题目标题通常为 *Division*,主要涉及二维费用背包问题或优化后的动态规划策略。题目大意是:给定一个整数数组,将其划分为若干个连续的子集,每个子集最多包含 $ m $ 个元素,并且每个子集的最大值与最小值之差不能超过给定的阈值 $ t $,目标是使所有子集的划分代价总和最小。每个子集的代价是该子集最大值与最小值的差值。 ### 动态规划思路 设 $ dp[i] $ 表示前 $ i $ 个元素的最小代价。状态转移方程如下: $$ dp[i] = \min_{j=0}^{i-1} \left( dp[j] + cost(j+1, i) \right) $$ 其中 $ cost(j+1, i) $ 表示从第 $ j+1 $ 到第 $ i $ 个元素构成一个子集的代价,即 $ \max(a[j+1..i]) - \min(a[j+1..i]) $。 为了高效计算 $ cost(j+1, i) $,可以使用滑动窗口或单调队列等数据结构来维护区间最大值与最小值,从而将时间复杂度优化到可接受的范围。 ### 示例代码 以下是一个简化版本的动态规划实现,使用暴力方式计算区间代价,适用于理解问题结构: ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 10010; int a[MAXN]; int dp[MAXN]; int main() { int T, n, m; cin >> T; for (int Case = 1; Case <= T; ++Case) { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = INF; int mn = a[i], mx = a[i]; for (int j = i; j >= max(1, i - m + 1); --j) { mn = min(mn, a[j]); mx = max(mx, a[j]); if (mx - mn <= T) { dp[i] = min(dp[i], dp[j - 1] + mx - mn); } } } cout << "Case " << Case << ": " << dp[n] << endl; } return 0; } ``` ### 优化策略 - **单调队列**:可以使用两个单调队列分别维护当前窗口的最大值与最小值,从而将区间代价计算的时间复杂度从 $ O(n^2) $ 降低到 $ O(n) $。 - **斜率优化**:若问题满足特定的决策单调性,可以考虑使用斜率优化技巧进一步加速状态转移过程。 ### 时间复杂度分析 原始暴力解法的时间复杂度为 $ O(n^2) $,在 $ n \leq 10^4 $ 的情况下可能勉强通过。通过单调队列优化后,可以稳定运行于 $ O(n) $ 或 $ O(n \log n) $。 ### 应用场景 HDU-3480 的问题模型可以应用于资源调度、任务划分等场景,尤其适用于需要控制子集内部差异的问题,如图像分块压缩、数据分段处理等[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值