1. 程式人生 > >2018/11/24

2018/11/24

hdu2102(bfs)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define
PI acos(-1.0) const int maxn = 1e5 + 10; char map[2][12][12]; int vis[2][12][12]; int dx[] = { 1,0,-1,0 }; int dy[] = { 0,1,0,-1 }; int N, M, T; struct node { int ce; int x; int y; int step; }; int flag; void bfs() { memset(vis, 0, sizeof vis); queue<node>que; node now; now.ce
= 0; now.x = now.y = 0; now.step = 0; que.push(now); vis[0][0][0]=1; flag = 0; while (!que.empty()) { now = que.front(); if (map[now.ce][now.x][now.y] == 'P' && now.step <= T) { flag = 1; return; }
if (map[now.ce][now.x][now.y] == 'P') return; for (int i = 0; i < 4; i++) { node next; next.x = now.x + dx[i]; next.y = now.y + dy[i]; next.ce = now.ce; int xx = next.x; int yy = next.y; int cece = next.ce; if (xx<0 || xx>=N) continue; if (yy<0 || yy>=M) continue; if (map[cece][xx][yy] == '*') continue; if (map[cece][xx][yy] == '#' && map[cece ^ 1][xx][yy] == '*') continue; if (map[cece][xx][yy] == '#' && map[cece ^ 1][xx][yy] == '#') continue; if (!vis[cece][xx][yy] && map[cece][xx][yy] == '#' && vis[cece ^ 1][xx][yy]==0) { vis[cece][xx][yy] = 1; next.ce ^= 1; vis[next.ce][next.x][next.y] = 1; next.step = now.step + 1; que.push(next); } if (!vis[cece][xx][yy] && map[cece][xx][yy] != '#') { vis[cece][xx][yy] = 1; next.step = now.step + 1; que.push(next); } } que.pop(); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; scanf("%d", &t); while (t--) { char ch; scanf("%d%d%d", &N, &M, &T); for (int i = 0; i < 2; i++) { for (int j = 0; j < N; j++) scanf("%s", map[i][j]); getchar(); } bfs(); if (flag) puts("YES"); else puts("NO"); } }
View Code

 

 

 codeforce1004B(思維,水) 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 500;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d",i%2);
    }
}
View Code

 

 

Friends HDU - 3172 (並查集+秩+map)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
const int maxn=100100;
map<string,int>p;
int pre[maxn],ran[maxn],maxx;
void init()
{
    int i;
    for(i=1;i<maxn;++i)
    {
        pre[i] = i;
        ran[i]=1;
    }
}

int find(int x)
{
    if(x!=pre[x])
    {
        pre[x] = find(pre[x]);
    }
    return pre[x];
}

void combine(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx!=fy)
    {
        pre[fx] = fy;
        ran[fy] += ran[fx];
    }
}


int main()
{
   
    int n,m;
    char name[30],na[30];
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
        p.clear();
        init();
        int k=1;
            scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%s %s",name,na);
            if(p.find(name)==p.end())
                p[name]=k++;
            if(p.find(na)==p.end())
                p[na]=k++;
            combine(p[name],p[na]);
            printf("%d\n",ran[find(p[na])]);
        }
        }
    }
    return 0;
}
View Code

 

 

 Divisibility by 25 CodeForces - 988E(大數模擬)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const long long int maxn=2e5+3;
string str;
int ans;
void solve(char a,char b)
{
    string s=str;
    int cnt1=s.rfind(a),cnt2=s.rfind(b);
    if(a=='0' && b=='0')
        cnt1=s.rfind(a,cnt1-1);
    if(cnt1==string::npos || cnt2==string::npos)
        return ;
    int cnt=0;
    if (cnt1 > cnt2)
    {
        cnt++;
        swap(cnt1, cnt2);
    }
    for (int i = cnt2; i+1 < s.size(); i++)
    {
        cnt++;
        swap(s[i], s[i+1]);
    }
    for (int i = cnt1; i+1 < s.size()-1; i++)
    {
        cnt++;
        swap(s[i], s[i+1]);
    }
    cnt += find_if(s.begin(), s.end(), [](char c){return c != '0';}) - s.begin();
    ans = min(ans, cnt);
}
    

int main()
{
    cin>>str;
    ans=INF;
    solve('0','0');
    solve('2','5');
    solve('5','0');
    solve('7','5');
    if(ans == INF)
        ans=-1;
    cout<<ans<<endl;
    return 0;
}
View Code