码迷,mamicode.com
首页 > 其他好文 > 详细

2018/11/24

时间:2018-11-24 14:19:04      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:sig   col   queue   printf   continue   图片   size   get   ecc   

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

 

2018/11/24

标签:sig   col   queue   printf   continue   图片   size   get   ecc   

原文地址:https://www.cnblogs.com/smallhester/p/10011473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!