HDU 2579 Dating with girls(2) BFS 余数判重

对于石头的处理就按照每个位置的时间取k的余数判一下重复就好,其他随意写

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 105;
const int maxk = 10;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
int d[maxn][maxn][maxk];
int n,m,k,sx,sy,ex,ey;
char mp[maxn][maxn];

void bfs() {
    queue<int> qx,qy,qk;
    qx.push(sx); qy.push(sy); qk.push(0);
    d[sx][sy][0] = 0;
    int x,y,nowk,nx,ny,nk;
    while(!qx.empty()) {
        x = qx.front(); y = qy.front(); nowk = qk.front();
        qx.pop(); qy.pop(); qk.pop();
        int nowt = d[x][y][nowk];
        for(int i = 0;i < 4;i++) {
            nx = x + dx[i]; ny = y + dy[i]; nk = (nowk + 1) % k;
            int &nt = d[nx][ny][nk];
            if((nk == 0 || mp[nx][ny] != ‘#‘) && nt > nowt + 1) {
                if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
                nt = nowt + 1;
                qx.push(nx); qy.push(ny); qk.push(nk);
            }
        }
    }
}

int main() {
    int T; scanf("%d",&T);
    while(T--) {
        memset(mp,‘#‘,sizeof(mp));
        memset(d,0x3f,sizeof(d));
        int inf = d[0][0][0];
        scanf("%d%d%d",&n,&m,&k);
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                scanf(" %c",&mp[i][j]);
                if(mp[i][j] == ‘Y‘) {
                    sx = i; sy = j;
                }
                if(mp[i][j] == ‘G‘) {
                    ex = i; ey = j;
                }
            }
        }
        bfs();
        int ans = inf;
        for(int i = 0;i < k;i++) ans = min(ans,d[ex][ey][i]);
        if(ans < inf) printf("%d\n",ans);
        else puts("Please give me another chance!");
    }
    return 0;
}

  

时间: 2024-10-15 17:35:14

HDU 2579 Dating with girls(2) BFS 余数判重的相关文章

HDU 2579 Dating with girls(2) (BFS)

Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2416    Accepted Submission(s): 690 Problem Description If you have solved the problem Dating with girls(1).I think you can

hdu 2579 Dating with girls(2)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and th

HDU 4474&amp;&amp;POJ 1465 BFS&amp;&amp;余数判重

两道题只是输入输出格式略有不同 给出n,m,m个数 要求生成一个数x,是n的倍数,并且只由这M个数构成(或不能含有这M个数中的任意一个),求最小的x,没有则输出-1(0): BFS找每一个满足n的倍数的数,用余数判重优化 对于给定两个数A,B,如果A和B模N都相同,设为C,即: A=x*N+C B=y*N+C 那么如果在A后面加上一个数字能够被N整除,则在B后面加上这个数字也肯定可以被N整除 即:(A*10+X)%N==(B*10+X)%N 因此可以利用模N的结果进行判重,只保留相同余数的最小数

[bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem

题意: 给一个n和m个数字(一位数) 求最小的n的倍数不含有这m个数字,不存在输出-1 思路: 首先有可能这个数超long long 所以无法暴力解决 所以这题应该是一个bfs 为什么能用余数判重呢 对于当前的余数进到队列里,一定是这个余数对应数的最小值 接下来再怎么添加到满足条件的后续东西应该是一样的 所以就可以余数判重了,类似数位dp的记录方式 然后再加上一个路径记录就好了 代码: #include"cstdlib" #include"cstdio" #incl

poj1465Multiple(经典BFS+余数判重)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6936   Accepted: 1495 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small

hdu 2579 Dating with girls(2) 【经典三维BFS】

Dating with girls(2)                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2443    Accepted Submission(s): 697 链接:http://acm.hdu.edu.cn/showp

11198 - Dancing Digits(BFS + hash判重)

题目:11198 - Dancing Digits 题目大意:每组数据给出8个数字,可能正可能负.要求最后将这8个数字按照数字绝对值从小到大的排序.排序的规则是让某个数字a邀请另一个数字b跳舞,这样a就可以插到b的左边或是右边,a能邀请b跳舞,则a* b <0 ,且a+b要是素数.题目问给出一组数据问能否通过邀请跳舞来排序,能的话就输出最少的邀请次数,否则输出-1. 解题思路:这题一开始竟然想着dfs,但是后面发现,这样的判断树可以是无限大,因为可以a邀请完b,然后b在邀请a,这样一来一回有可能

HDU 2578 Dating with girls(1) [补7-26]

Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1196 Problem Description Everyone in the HDU knows that the number of boys is larger than the

hdu 2578 Dating with girls(1)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with