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

来源:互联网 发布:淘宝 多隆 编辑:程序博客网 时间:2024/06/06 17:48

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.###########4N 2S 1E 1W 2
output
AD
input
3 4#####.A#####2W 1N 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");    }}












阅读全文
0 0
原创粉丝点击