ZOJ 2925 DomiNo Grid

广搜。。深搜是不行的。。

深搜和广搜搜索的顺序不一样。。

一开始 深搜 wa死了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-8
#define LL long long 
#define ull unsigned long long
#define MP make_pair
#define mnx 505
int n, m;
int d[8][3][2] = { -1, 0, -1, -1, -1, 1,  // 'N'
                   1, 0, 1, -1, 1, 1,     // 'S'
				   0, 1, -1, 1, 1, 1,     // 'E'
				   0, -1, -1, -1, 1, -1,  // 'W'
				   -1, -1, 0, -1, -1, 0,  // 'V'
				   -1, 1, 0, 1, -1, 0,    // 'Y'
				   1, 1, 1, 0, 0, 1,      // 'Q'
				   1, -1, 1, 0, 0, -1};   // 'J'
char ch[mnx][mnx];    
int qx[500005], qy[500005];    //wa了好多次,后来发现是队列开太小了。。至少要 mnx * mnx 
char dr[8] = {'N', 'S', 'E', 'W', 'V', 'Y', 'Q', 'J'}, qc[500005];
char alp[3][3] = {'V', 'N', 'Y', 'W', ' ', 'E', 'J', 'S', 'Q'};
// -1, 0 代表N, 由于数组的下标不能为负, 就用 [0][1] 代表'N'了。。其他的也是一样 
// -1, -1 代表 V, [0][0] 就代表 V 
void bfs( int a, int b, char str ){
	int k, head = 0, tail = 0;
	ch[a][b] = str;
	qx[tail] = a, qy[tail] = b, qc[tail++] = str;
	while( head < tail ){
		int sx = qx[head], sy = qy[head]; char c = qc[head++];
		for( int i = 0; i < 8; i++ ){
		    if( dr[i] == c ){
		    	k = i; break;
		    }
	    }
	    for( int i = 0; i < 3; i++ ){
	    	int a0 = d[k][i][0], b0 = d[k][i][1];
	    	int bx = sx + a0, by = sy + b0;
			if( bx > 0 && by > 0 && bx <= n && by <= m && ch[bx][by] == 'X' ){
				ch[bx][by] = alp[a0+1][b0+1];
				qx[tail] = bx, qy[tail] = by, qc[tail++] = ch[bx][by];
			} 
	    }
	}
} 
int main(){
    int ok = 1;
    while( scanf("%d%d", &n, &m) != EOF ){
        if( ok == 1 )  ok = 0;               // 这里 PE了好几次。。主要是题目没有看清楚 
        else cout<<endl;
        for( int i = 1; i <= n; i++ )
        	for( int j = 1; j<= m; j++ )
        		cin>>ch[i][j];
        char c;
        int a, b;
        cin>>a>>b>>c;
        bfs(a, b, c);
        for( int i = 1; i <= n; i++ ){
        	for( int j = 1; j<= m; j++ ){
        		cout<<ch[i][j];
        	}
        	cout<<endl;
        }
   
    }
    return 0;
}

代码不难写。。不过,哎,细心啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值