Educational Codeforces Round 1 (D) 搜索(优化剪枝)

来源:互联网 发布:上海证券软件下载 编辑:程序博客网 时间:2024/05/21 15:46

D. Igor In the Museum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor is in the museum and he wants to see as many pictures as possible.

Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

Input

First line of the input contains three integers nm and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

Sample test(s)
input
5 6 3*******..*.********....*******2 22 54 3
output
6410
input
4 4 1*****..**.******3 2
output
8


题意:一个人在博物馆看画,他只能看到相邻的画(上下左右),人可以移动,“.”代表地面,“*”代表墙(墙上有画),问当人在某一个点,他最多可以看到多少画(人是可以移动的,不可以穿墙)。



题解:好吧,这一题敲了接近3个小时,大哭,不过终于还是优化出来了,开始题意看错了尴尬不说了。



1.开始当然是想到暴力搜索,搞起来,每查询一次,就搜一次。提交超时,一下为超时代码


#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define N 1005char s[N][N];int vis[N][N];int n,m;struct point{    int x,y;    point (int _x,int _y):x(_x),y(_y){}    point (){}};vector<point>eg;vector<point>eg2;int vx[]={0,0,1,-1};int vy[]={1,-1,0,0};int ans;void dfs(int x,int y){    if(s[x][y]=='*')    {        ans++;        return ;    }    else if(s[x][y]=='#')    {        return ;    }    else if (s[x][y]=='.')    {        eg2.push_back(point(x,y));        s[x][y]='#';        for(int i=0;i<4;i++)        {            int xx=x+vx[i],yy=y+vy[i];            if(xx<0||yy<0||xx>=n||yy>=m)continue;            dfs(xx,yy);        }    }    return ;}int main(){    int k;    int x,y;    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<n;i++)        scanf("%s",s[i]);    while(k--)    {        eg2.clear();        eg.clear();        ans=0;        scanf("%d%d",&x,&y);        dfs(x-1,y-1);        printf("%d\n",ans);        for(int i=0;i<eg.size();i++)        {            s[eg[i].x][eg[i].y]='*';        }        for(int i=0;i<eg2.size();i++)        {            s[eg2[i].x][eg2[i].y]='.';        }    }    return 0;}



2。我想不行啊,每次都搜一遍太耗时间了,先吧所有的情况都搜出来,打个表,直接输出。又 超时了。。。超时代码


#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define N 2005char s[N][N];int n,m;struct point{    int x,y;    point (int _x,int _y):x(_x),y(_y){}    point (){}};vector<point>eg;vector<point>eg2;int vx[]={0,0,1,-1};int vy[]={1,-1,0,0};int answer[N][N];int ans;void dfs(int x,int y){    if(s[x][y]=='*')    {        ans++;        return ;    }    else if(s[x][y]=='#')    {        return ;    }    else if (s[x][y]=='.')    {        eg2.push_back(point(x,y));        s[x][y]='#';        for(int i=0;i<4;i++)        {            int xx=x+vx[i],yy=y+vy[i];            if(xx<0||yy<0||xx>=n||yy>=m)continue;            dfs(xx,yy);        }    }    return ;}int main(){    int k;    int x,y;    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<n;i++)        scanf("%s",s[i]);   for(int i=0;i<n;i++)   for(int j=0;s[i][j];j++)    if(s[i][j]=='.')    {        eg2.clear();        ans=0;        dfs(i,j);        answer[i][j]=ans;        for(int i=0;i<eg2.size();i++)        {            s[eg2[i].x][eg2[i].y]='.';        }    }    while(k--)    {        scanf("%d%d",&x,&y);        printf("%d\n",answer[x-1][y-1]);    }    return 0;}


3.好了,思考了一下发现,在同一个区域的点根本不需要重新搜,可以直接把这个区域当成一个集合(一个块),在这个集合里的所有的点的答案都是一样的啊。这样一个区域块就只需要搜一遍了,减去了无用点的搜索。嗯,于是这样处理,在搜索过程中把这个区域里的所有的点的坐标信息都存入数组里,然后搜索结束一一对这些点坐标赋值,然后终于是过了,一下AC代码



#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define N 2005char s[N][N];int n,m;struct point{    int x,y;    point (int _x,int _y):x(_x),y(_y){}    point (){}};vector<point>eg;vector<point>eg2;int vx[]={0,0,1,-1};int vy[]={1,-1,0,0};int answer[N][N];int ans;void dfs(int x,int y){    if(s[x][y]=='*')    {        ans++;        return ;    }    else if(s[x][y]=='#')    {        return ;    }    else if (s[x][y]=='.')    {        eg2.push_back(point(x,y));        s[x][y]='#';        for(int i=0;i<4;i++)        {            int xx=x+vx[i],yy=y+vy[i];            if(xx<0||yy<0||xx>=n||yy>=m)continue;            dfs(xx,yy);        }    }    return ;}int main(){        int k;    int x,y;    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<n;i++)        scanf("%s",s[i]);   for(int i=0;i<n;i++)   for(int j=0;s[i][j];j++)    if(s[i][j]=='.')    {        eg2.clear();        ans=0;        dfs(i,j);        answer[i][j]=ans;        for(int i=0;i<eg2.size();i++)        {            answer[eg2[i].x][eg2[i].y]=ans;        }    }    while(k--)    {        scanf("%d%d",&x,&y);        printf("%d\n",answer[x-1][y-1]);    }    return 0;}















0 0
原创粉丝点击