国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

The Game

2019-11-14 11:14:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

One morning, you wake up and think: "I am such a good PRogrammer. Why not make some money?'' So you decide to write a computer game. The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture. One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties: It consists of straight segments, each one being either horizontal or vertical. It does not cross any other game pieces. (It is allowed that the path leaves the board temporarily.) Here is an example: 
The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.InputThe input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a "X" if there is a game piece at this location, and a space if there is no game piece. Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0". The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.OutputFor each board, output the line "Board #n:", where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with "Pair m: ", where m is the number of the pair (starting the count with 1 for each board). Follow this by "ksegments.", where k is the minimum number of segments for a path connecting the two game pieces, or "impossible.", if it is not possible to connect the two game pieces as described above. Output a blank line after each board.Sample Input
5 4XXXXXX   XXXX X XXX 2 3 5 31 3 4 42 3 3 40 0 0 00 0Sample Output
Board #1:Pair 1: 4 segments.Pair 2: 3 segments.Pair 3: impossible.

解題報(bào)告

直接廣搜就行了,由隊(duì)列的每個(gè)點(diǎn)向四個(gè)方向標(biāo)記直到遇到'X',小心每組結(jié)束還有個(gè)/n,

#include<stdio.h>#include<string.h>#include<queue>#define MAX_N 80#define INF 0x3f3f3f3fusing namespace std;char map[MAX_N][MAX_N];int dp[MAX_N][MAX_N];const int ox[]={0,0,1,-1};const int oy[]={1,-1,0,0};int w,h;int s_x,s_y,e_x,e_y;int bfs(){    memset(dp,0x3f,sizeof(dp));    queue<pair<int,int> > que;    que.push(make_pair(s_x,s_y));    dp[s_y][s_x]=0;    map[e_y][e_x]=' ';    int H=h+1,W=w+1;        while(!que.empty()){        int X=que.front().first,Y=que.front().second;que.pop();        int S=dp[Y][X]+1;        for(int i=0;i<4;i++){            int x=X,y=Y;            while(true){                x+=ox[i],y+=oy[i];                if(x<0||x>W||y<0||y>H||map[y][x]=='X') break;                if(dp[y][x]<=S) continue;                que.push(make_pair(x,y));                dp[y][x]=S;            }        }    }    map[e_y][e_x]='X';    return dp[e_y][e_x];}int main(){    int Board=1,Pair;    while(~scanf("%d%d",&w,&h)&&w&&h){        memset(map,0,sizeof(map));        getchar();        for(int i=1;i<=h;i++)            gets(map[i]+1);        printf("Board #%d:/n",Board++);Pair=1;        while(scanf("%d%d%d%d",&s_x,&s_y,&e_x,&e_y)&&s_x&&s_y&&e_x&&e_y){            printf("Pair %d: ",Pair++);            int ans=bfs();            if(ans==INF) puts("impossible.");            else printf("%d segments./n",ans);        }        putchar('/n');    }    return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 治多县| 林口县| 军事| 岑溪市| 威宁| 金堂县| 徐闻县| 凤城市| 渑池县| 山阳县| 浑源县| 迭部县| 汉源县| 鹿邑县| 黄山市| 维西| 石台县| 昌平区| 龙陵县| 辽阳市| 安化县| 明水县| 屏南县| 杨浦区| 云浮市| 郧西县| 渭源县| 远安县| 元谋县| 涿州市| 琼结县| 肇庆市| 敦化市| 鸡西市| 隆回县| 新建县| 余姚市| 宿松县| 巴林左旗| 兰考县| 定结县|