D - DomiNo GridDominoes are small, flat, rectangular-shaped game pieces. Domino pieces are usually twice as long as they are wide and are usually made to be exactly half as thick as they are wide so that they can stand on edge without falling over. If we push one end of a queue of dominoes, the whole queue will fall over.

Now, you will be given some descriptions of domino grid with a '.' indicating an open space and an uppercase 'X' indicating a domino and the force used on one domino. You are to compute the ending descriptions. The force consists of two parts : location and direction. There are 8 directions shown below.
direction abbreviation
West : W
NorthWest : V
North : N
NorthEast : Y
East : E
SouthEast : Q
South : S
SouthWest : J
The falling direction of the pushed domino is always the same as the force. Other dominoes will fall over if: 1) it's adjacent with a previous fallen domino. 2) it's within 45 degree of the falling direction of the previous domino.
The direction of falling is the relative position of it to the previous fallen domino. No two dominoes will cause the same domino to fall over simultaneously. See the following example for more details.
XXXXXX
XXX
We say the outer 8 dominoes are adjacent with the middle one. With a force to east on the middle domino, the 3 dominoes in the third column will fall over and the direction will be northeast, east, southeast. So the ending grid is :
XXYXEE
XXQ
Input
There are multiple test cases. Each case begins with a line containing two positive integer n and m (1 <= n, m <= 500) that are the number of rows and columns of the grid. The next n lines each with m chars (only '.' and 'X') describe one row of the grid. At last, two integers i, j (ith row, jth column, both i and j start from 1) indicate the location of the force and a char C describes the direction of the force. You can assume that there is a domino at the location (i, j).
Process to the end of file.
Output
Print the ending description of the grid, using the abbreviations for the fallen dominoes.
Print a blank line between cases.
Sample Input
2 4
..XX
XX..
1 3 S
4 4
XXX.
...X
X..X
XXX.
3 1 E
Sample Output
..SX
WJ..
WWV.
...N
E..Y
XQE.
自己写的代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
#define mxn 150
#define mxe 10010
#define inf 0x3f3f3f3f
#define LL long long
#define mod 1000000007
char s[550][550];
bool vis[550][550];
int n,m;
struct node
{ char d;
int x,y;
};
void bfs(int x,int y, char dir)
{ queue<node> q;
node a;
a.d=dir; a.x=x; a.y=y;
vis[x][y]=1;
q.push(a);
while(!q.empty())
{ a=q.front();
s[a.x][a.y]=a.d;
node nn[3];
switch(a.d)
{ case 'E' : { nn[0].x=a.x;nn[1].x=a.x-1;nn[2].x=a.x+1;
nn[0].y=a.y+1; nn[1].y=a.y+1; nn[2].y=a.y+1;
nn[0].d=a.d; nn[1].d='Y'; nn[2].d='Q';
break;
}
case 'Y': { nn[0].x=a.x-1; nn[1].x=a.x-1; nn[2].x=a.x;
nn[0].y=a.y+1; nn[1].y=a.y; nn[2].y=a.y+1;
nn[0].d=a.d; nn[1].d='N'; nn[2].d='E';
break;
}
case 'N': { nn[0].x=a.x-1; nn[1].x=a.x-1; nn[2].x=a.x-1;
nn[0].y=a.y; nn[1].y=a.y-1; nn[2].y=a.y+1;
nn[0].d=a.d; nn[1].d='V'; nn[2].d='Y';
break;
}
case 'V': { nn[0].x=a.x-1; nn[1].x=a.x; nn[2].x=a.x-1;
nn[0].y=a.y-1; nn[1].y=a.y-1; nn[2].y=a.y;
nn[0].d=a.d; nn[1].d='W'; nn[2].d='N';
break;
}
case 'W': { nn[0].x=a.x; nn[1].x=a.x+1; nn[2].x=a.x-1;
nn[0].y=a.y-1; nn[1].y=a.y-1; nn[2].y=a.y-1;
nn[0].d=a.d; nn[1].d='J'; nn[2].d='V';
break;
}
case 'J': { nn[0].x=a.x+1; nn[1].x=a.x+1; nn[2].x=a.x;
nn[0].y=a.y-1; nn[1].y=a.y; nn[2].y=a.y-1;
nn[0].d=a.d; nn[1].d='S'; nn[2].d='W';
break;
}
case 'S': { nn[0].x=a.x+1; nn[1].x=a.x+1; nn[2].x=a.x+1;
nn[0].y=a.y; nn[1].y=a.y+1; nn[2].y=a.y-1;
nn[0].d=a.d; nn[1].d='Q'; nn[2].d='J';
break;
}
case 'Q': { nn[0].x=a.x+1; nn[1].x=a.x+1; nn[2].x=a.x;
nn[0].y=a.y+1; nn[1].y=a.y; nn[2].y=a.y+1;
nn[0].d=a.d; nn[1].d='S'; nn[2].d='E';
break;
}
}
int i;
for (i=0;i<=3;i++)
{ if (nn[i].x>0 && nn[i].x<=n && nn[i].y>0 && nn[i].y<=m && s[nn[i].x][nn[i].y]=='X' && vis[nn[i].x][nn[i].y]==0 )
{ vis[nn[i].x][nn[i].y]=1;
q.push(nn[i]);
}
}
q.pop();
}
}
int main()
{ int i,j,x,y;
char dir;
bool bl=1;
while(scanf("%d%d",&n,&m)!=EOF)
{ getchar();
for(i=1;i<=n;i++)
{ for ( j=1;j<=m;j++ )
scanf("%c",&s[i][j]);
getchar();
}
scanf("%d%d",&x,&y);
getchar();
scanf("%c",&dir);
memset(vis,0,sizeof(vis));
bfs(x,y,dir);
if (bl) bl=0;
else printf("\n");
for(i=1;i<=n;i++)
{ for ( j=1;j<=m;j++ )
printf("%c",s[i][j]);
printf("\n");
}
}
return 0;
}
本文介绍了一种模拟多米诺骨牌翻倒过程的算法。该算法通过广度优先搜索来模拟骨牌受力后的翻倒方向及连锁反应,最终生成网格中每个骨牌的翻倒状态。输入包括骨牌网格布局、施力位置和方向,输出为模拟后的网格布局。

1315

被折叠的 条评论
为什么被折叠?



