CSU1569: Wet Tiles(BFS控制时间相同)

1569: Wet Tiles

Time Limit: 60 Sec  Memory Limit: 512 MB

Submit: 73  Solved: 34

[Submit][Status][Web
Board
]

Description

Alice owns a construction company in the town of Norainia, famous for its unusually dry weather. In fact, it only rains a few days per year there. Because of this phenomenon, many residents of Norainia neglect to do roof repairs until leaks occur and ruin their
floors. Every year, Alice receives a deluge of calls from residents who need the leaks fixed and floor tiles replaced. While exquisite in appearance, Norainia floor tiles are not very water resistant; once a tile becomes wet, it is ruined and must be replaced.
This year, Alice plans to handle the rainy days more efficiently than in past years. She will hire extra contractors to dispatch as soon as the calls come in, so hopefully all leaks can be repaired as soon as possible. For each house call, Alice needs a program
to help her determine how many replacement tiles a contractor team will need to bring to complete the job.

For a given house, square floor tiles are arranged in a rectangular grid. Leaks originate from one or more known source locations above specific floor tiles. After the first minute, the tiles immediately below the leaks are ruined. After the second minute,
water will have spread to any tile that shares an edge with a previously wet tile. This pattern of spreading water continues for each additional minute. However, the walls of a house restrict the water; if a damaged area hits a wall, the water does not penetrate
the wall. We assume there are always four outer walls surrounding the entire house. A house may also have a number of additional "inner" walls; each inner wall is comprised of a connected linear sequence of locations (which may or may not be connected to the
outer walls or to each other).

As an example, Figure 1 shows water damage (in gray) that would result from three initial leaks (each marked with a white letter ‘L‘) after each of the first five minutes of time. Tiles labeled ‘2‘ become wet during the second minute, tiles labeled ‘3‘ become
wet during the third minute, and so forth. The black areas designate inner walls that restrict the flow of water. Note that after 5 minutes, a total of 75 tiles have been damaged and will need to be replaced. Figures 2 through 4 show other houses that correspond
to the example inputs for this problem.

75 wet tiles

17 wet tiles

4 wet tiles

94 wet tiles

Input

Each house is described beginning with a line having five integral parameters: X Y T L W. Parameters X and Y designate the dimensions of the rectangular grid, with 1 ≤ X ≤ 1000 and 1 ≤ Y ≤ 1000. The coordinate system is one-indexed, as shown in the earlier
figures. Parameter T designates the number of minutes that pass before a team of contractors arrives at a house and stops the leaks, with 1 ≤ T ≤ 200000. The parameter L designates the number of leaks, with 1 ≤ L ≤ 100. Parameter W designates the number of
inner walls in the house, 0 ≤ W ≤ 100.

The following 2L integers in the data set, on one or more lines, are distinct (x y) pairs that designate the locations of the L distinct leaks, such that 1 ≤ x ≤ X and 1 ≤ y ≤ Y.

If W > 0, there will be 4W additional integers, on one or more lines, that describe the locations of the walls. For each such wall the four parameters (x1,y1), (x2,y2) describe the locations of two ends of the wall. Each wall replaces a linear sequence of adjoining
tiles and is either axis-aligned or intersects both axes at a 45 degree angle. Diagonal walls are modeled as a sequence of cells that would just be touching corner to corner. If the two endpoints of a wall are the same, the wall just occupies the single cell
at that location. Walls may intersect with each other, but no leak is over a wall.

There will be one or more houses in the data file and a line with a single integer -1 designates the end of the data set.

Output

For each house, display the total number of tiles that are wet after T minutes.

Sample Input

12 12 5 3 5
2 11 3 3 9 5
1 9 6 9 1 7 4 4 7 1 7 4
10 9 10 12 11 4 12 4
9 7 8 1 3
4 3
2 2 6 6 6 2 2 6 8 2 8 2
6 7 50 1 3
3 4
2 2 2 6 3 6 5 4 5 4 3 2
12 12 5 3 0
2 11 3 3 9 5
-1

Sample Output

75
17
4
94

HINT

Source

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N = 1005;
struct locate
{
    int x,y;
};
int mapt[N][N],n,m,T,dir[4][2]={0,1,0,-1,1,0,-1,0};
queue<locate>q[2];
void bfs(int flag)
{
    locate pre,now;
    while(!q[flag].empty())
    {
        pre=q[flag].front();q[flag].pop();
        for(int e=0;e<4;e++)
        {
            now.x=pre.x+dir[e][0];
            now.y=pre.y+dir[e][1];
            if(now.x>0&&now.x<=n&&now.y>0&&now.y<=m&&mapt[now.x][now.y]==0)
            {
                mapt[now.x][now.y]=1;
                q[!flag].push(now);
            }
        }
    }
}
int main()
{
    int L,W;
    locate now,pre;
    while(scanf("%d",&n)>0&&n!=-1)
    {
        scanf("%d%d%d%d",&m,&T,&L,&W);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        mapt[i][j]=0;
        while(!q[0].empty())q[0].pop();
        while(!q[1].empty())q[1].pop();
        while(L--)
        {
            scanf("%d%d",&now.x,&now.y); q[0].push(now);
        }
        while(W--)
        {
            scanf("%d%d%d%d",&pre.x,&pre.y,&now.x,&now.y);
            if(now.x>pre.x)
            {
                int tt;
                tt=pre.x; pre.x=now.x; now.x=tt;
                tt=pre.y; pre.y=now.y; now.y=tt;
            }
            if(pre.x==now.x)
            {
                if(pre.y<now.y)
                {
                    int tt=pre.y; pre.y=now.y; now.y=tt;
                }
                while(pre.y>=now.y)
                {
                    mapt[pre.x][pre.y--]=2;
                }
            }
            else if(pre.y==now.y)
            {
                if(pre.x<now.x)
                {
                    int tt=pre.x; pre.x=now.x; now.x=tt;
                }
                while(pre.x>=now.x)
                {
                    mapt[pre.x--][pre.y]=2;
                }
            }
            else if(pre.x>=now.x&&pre.y<=now.y)
            {
                while(now.x<=pre.x&&now.y>=pre.y)
                {
                    mapt[now.x][now.y]=2;
                    now.y--; now.x++;
                }
            }
            else
            {
                while(now.x<=pre.x&&now.y<=pre.y)
                {
                    mapt[pre.x][pre.y]=2;
                    pre.y--; pre.x--;
                }
            }
        }

        int flag=0;
        while(!q[flag].empty())
        {T--;
            if(T==0) break;

            bfs(flag); flag=!flag;
        }
        int ans=0;
        for(int j=m;j>=1;j--)
        {
            for(int i=1;i<=n;i++)
            {
                //printf("%d ",mapt[i][j]);
                if(mapt[i][j]==1)
                ans++;
            }
            //printf("\n");
        }

        printf("%d\n",ans);
    }
}
时间: 2024-10-29 00:53:44

CSU1569: Wet Tiles(BFS控制时间相同)的相关文章

访问调度控制 时间控件

Visulalization Voronoi in OpenSceneGraph [email protected] Abstract. In mathematics a Voronoi diagram is a way of dividing space into a number of regions. A set of points, called seeds, sites, or generators is specified beforehand and for each seed t

线程控制时间的随笔(实例)

1 package kk; 2 import java.util.*; 3 import java.awt.EventQueue; 4 import java.text.DecimalFormat; 5 import java.text.SimpleDateFormat; 6 import java.util.Scanner; 7 import java.util.Timer; 8 import java.util.TimerTask; 9 10 import javax.swing.JOpti

Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:

---------------------------------------------------------------------------------------------------------------------------------------- QTime::QTime() 默认构造函数,构造一个时,分,秒都为0的时间,如00:00:00.000(午夜) QTime::QTime(int h, int m, int s=0, int ms = 0) 构造一个用户指定时

有关JS控制时间的几个小Demo

一.Document自带的定时和延时方法: 循环执行:var timeid = window.setInterval("方法名或方法","延时");window.clearInterval(timeid); 定时执行:var tmid = window.setTimeout("方法名或方法", "延时");window.clearTimeout(tmid); 比如: //循环执行,每隔3秒钟执行一次showalert() wi

easyUI的dateBox控制时间格式

<input type='text' name='yearQuery' class='easyui-datebox ' data-options="formatter:myformatter,parser:myparser"   /> <javascript> function myformatter(date){        var y = date.getFullYear();        var m = date.getMonth()+1;      

java定时器控制时间打印

1 public class test2 { 2 public static void main(String []args){ 3 Timer timer=new Timer(); 4 timer.schedule(new TimerTask(){ 5 6 7 @Override 8 public void run() { 9 // TODO Auto-generated method stub 10 SimpleDateFormat sdf = new SimpleDateFormat("y

JS游戏控制时间代码

var canvas = new HGAME.canvas();var testBox=document.getElementById('boxRender');testBox.appendChild(canvas.dom);var animate=new HGAME.animate({ action:function(){ canvas.render(); }});var colorObj={ r:0, g:0, b:0};function initColor(){ var div=null;

Tuxedo 超时时间控制(转贴)

以下是转贴: ----------------------------------------------------------------------------------------------------------------------------------------------- 源于才文章确实详细,暂且转载于此,谢原发帖主人. Tuxedo 超时控制(转贴)原帖发于DEV2DEV,现转贴在此. TUXEDO超时控制全功略 摘要: 本<功略>集中了TUXEDO应用中,可能涉

iOS CoreAnimation专题——原理篇(四)动画时间控制

前言 CAMediaTiming协议 可视化的CAMediaTiming协议 beginTime fillMode autoreverses repeatCount repeatDuration speed 动画速度的分层表示 CAMediaTiming协议的其他实现 timeOffset 更多的动画时间可视化插图 控制动画时间 Slider 关于fillMode和Ease的补充 fillMode Ease 后记 前言 这一章虽然叫做动画时间控制,然而我们并不会去深入到一般的动画时间中,我们将讨