Codeforces 106D Treasure Island【思维+二维前缀和】

D. Treasure Island
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map.

The treasure map can be represented as a rectangle n × m in size. Each cell stands for an islands' square (the square's side length equals to a mile). Some cells stand for the sea and they are impenetrable. All other cells are penetrable (i.e. available) and some of them contain local sights. For example, the large tree on the hills or the cave in the rocks.

Besides, the map also has a set of k instructions. Each instruction is in the following form:

"Walk n miles in the y direction"

The possible directions are: north, south, east, and west. If you follow these instructions carefully (you should fulfill all of them, one by one) then you should reach exactly the place where treasures are buried.

Unfortunately the captain doesn't know the place where to start fulfilling the instructions — as that very piece of the map was lost. But the captain very well remembers that the place contained some local sight. Besides, the captain knows that the whole way goes through the island's penetrable squares.

The captain wants to know which sights are worth checking. He asks you to help him with that.

Input

The first line contains two integers n and m (3 ≤ n, m ≤ 1000).

Then follow n lines containing m integers each — the island map's description. "#" stands for the sea. It is guaranteed that all cells along the rectangle's perimeter are the sea. "." stands for a penetrable square without any sights and the sights are marked with uppercase Latin letters from "A" to "Z". Not all alphabet letters can be used. However, it is guaranteed that at least one of them is present on the map. All local sights are marked by different letters.

The next line contains number k (1 ≤ k ≤ 105), after which k lines follow. Each line describes an instruction. Each instruction possesses the form "dir len", where dir stands for the direction and len stands for the length of the way to walk. dir can take values "N", "S", "W" and "E" for North, South, West and East correspondingly. At that, north is to the top, South is to the bottom, west is to the left and east is to the right. len is an integer from 1 to 1000.

Output

Print all local sights that satisfy to the instructions as a string without any separators in the alphabetical order. If no sight fits, print "no solution" without the quotes.

Examples
input
6 10
##########
#K#..#####
#.#..##.##
#..L.#...#
###D###A.#
##########
4
N 2
S 1
E 1
W 2
output
AD
input
3 4
####
#.A#
####
2
W 1
N 2
output
no solution

题目大意:


给出一个N*M的地图,现在最多有26个人,分别用26个字母表示分落在地图各处,#代表墙,现在有q个操作,表示让每个人按照没一轮次都按照要求方向走len步,问哪几个人可以将所有操作都走完的过程中,不会撞墙。


思路:


显然按照Bfs去模拟的话,会TLE。我们考虑问题的特点。

我们知道,每一次操作都是按照一个方向去走的,我们要判定的问题只有是否撞墙这一个点。

那么我们不妨预处理出4个前缀和,N【i】【j】表示在(i,j)这个点上,往北走多少步之内不会撞墙。

其他3个方向同理即可。

那么我们只需要O(26*q)去模拟判定即可。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
char a[1500][1500];
int N[1500][1500];
int S[1500][1500];
int E[1500][1500];
int W[1500][1500];
char op[150000][3];
int move[150000];
int q,ok,n,m;
int Slove(int x,int y)
{
    for(int i=1;i<=q;i++)
    {
        if(op[i][0]=='N')
        {
            if(N[x][y]-move[i]>=0)
            {
                x-=move[i];
            }
            else return 0;
        }
        if(op[i][0]=='S')
        {
            if(S[x][y]-move[i]>=0)
            {
                x+=move[i];
            }
            else return 0;
        }
        if(op[i][0]=='W')
        {
            if(W[x][y]-move[i]>=0)
            {
                y-=move[i];
            }
            else return 0;
        }
        if(op[i][0]=='E')//dong
        {
            if(E[x][y]-move[i]>=0)
            {
                y+=move[i];
            }
            else return 0;
        }
    }
    ok=1;
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ok=0;
        for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[j][i]=='#')N[j][i]=0;
                else N[j][i]=N[j-1][i]+1;
            }
            for(int j=n;j>=1;j--)
            {
                if(a[j][i]=='#')S[j][i]=0;
                else S[j][i]=S[j+1][i]+1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='#')W[i][j]=0;
                else W[i][j]=W[i][j-1]+1;
            }
            for(int j=m;j>=1;j--)
            {
                if(a[i][j]=='#')E[i][j]=0;
                else E[i][j]=E[i][j+1]+1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                N[i][j]--;
                S[i][j]--;
                W[i][j]--;
                E[i][j]--;
            }
        }
        scanf("%d",&q);
        for(int i=1;i<=q;i++)scanf("%s%d",op[i],&move[i]);
        for(int z=1;z<=26;z++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(a[i][j]-'A'+1==z)
                    {
                       int tmp= Slove(i,j);
                       if(tmp==1)printf("%c",a[i][j]);
                    }
                }
            }
        }
        if(ok==0)printf("no solution\n");
        printf("\n");
    }
}












### Codeforces二维前缀和的应用 在解决涉及矩阵区域查询的问题时,二维前缀和是一种非常有效的工具。通过预先计算部分和,可以在常数时间内快速获取任意子矩形内的元素总和。 #### 什么是二维前缀和? 对于一个大小为 \( m \times n \) 的矩阵 `A`,定义其对应的前缀和矩阵 `sum` 如下: \[ sum[i][j] = A[0...i-1][0...j-1] \] 即 `sum[i][j]` 表示从原点 `(0, 0)` 到位置 `(i-1, j-1)` 所构成的矩形区域内所有元素之和[^2]。 为了方便处理边界情况,通常会将索引偏移一位,在实际编程中使用 `sum[i+1][j+1]` 来表示上述范围内的累加值。 #### 计算方法 构建前缀和的过程可以通过双重循环完成,时间复杂度为 O(mn),其中 m 和 n 分别代表矩阵的高度和宽度。核心代码如下所示: ```cpp for(int i = 1; i <= h; ++i){ for(int j = 1; j <= w; ++j){ // 当前格子加上左边、上面以及左上的三个方向已经累积的结果 sum[i][j] = A[i-1][j-1] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]; } } ``` 这里需要注意减去重复计算的部分 `- sum[i-1][j-1]`,因为这部分被前面两次相加操作多算了。 #### 查询指定矩形区域的和 假设要查询以坐标 `(x1,y1)` 作为左上角顶点,`(x2,y2)` 作为右下角顶点所围成的小矩形内部数值总和,则可以按照下面的方式进行计算: \[ query(x_1, y_1, x_2, y_2)=sum[x_2][y_2]-sum[x_1-1][y_2]-sum[x_2][y_1-1]+sum[x_1-1][y_1-1]\] 这同样遵循了容斥原理来排除重叠部分的影响。 #### 实际应用案例 考虑这样一个题目:“在一个整数矩阵中找到满足特定条件的最大/最小面积”。这类问题往往需要频繁地对不同尺寸的子矩形做求和运算,而借助于预处理好的前缀和表就可以大大简化这些操作并提高效率。 例如,在某些情况下可能还需要结合其他数据结构如线段树或者二分查找来进行更复杂的优化;而在另一些场景里则可以直接利用简单的四边形不等式性质加速搜索过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值