中秋放假这两天写的几个题目

版权声明:分享、借鉴或使用刁肥宅的代码请务必注明出处!刁肥宅保留追究代码剽窃者的一切权利! https://blog.csdn.net/u25th_engineer/article/details/82828213

       题目1:B. Vasya and Cornfield——https://codeforces.com/contest/1058/problem/B

       AC代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, d, m, ans;
    int x1, y1, x2, y2, x3, y3, x4, y4;
    int l1, l2, l3, l4;
    double S, s1, s2, s3, s4, x, y;
    while( ~scanf( "%d%d", &n, &d ) )
    {
        x1 = 0, y1 = d;
        x2 = d, y2 = 0;
        x3 = n, y3 = n - d;
        x4 = n - d, y4 = n;
        S = s1 = s2 = s3 = s4 = 0;
        S = sqrt( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) * sqrt( ( x2 - x3 ) * ( x2 - x3 ) + ( y2 - y3 ) * ( y2 - y3 ) );
        scanf( "%d", &m );
        for( int i = 0; i < m; i ++ )
        {
            scanf( "%lf%lf", &x, &y );
            {
                s1 = (double)( ( x1 - x ) * ( y2 - y ) - ( x2 - x ) * ( y1 - y ) ) / 2;
                s2 = (double)( ( x2 - x ) * ( y3 - y ) - ( x3 - x ) * ( y2 - y ) ) / 2;
                s3 = (double)( ( x3 - x ) * ( y4 - y ) - ( x4 - x ) * ( y3 - y ) ) / 2;
                s4 = (double)( ( x4 - x ) * ( y1 - y ) - ( x1 - x ) * ( y4 - y ) ) / 2;
            }
            ///printf( "s1 = %lf,\ts2 = %lf,\ts3 = %lf,\ts4 = %lf,\ts1 + s2 + s3 + s4 = %lf,\tabs(s1) + abs(s2) + abs(s3) + abs(s4) = %lf,\tS = %lf\n", s1, s2, s3, s4, s1 + s2 + s3 + s4, abs(s1) + abs(s2) + abs(s3) + abs(s4), S );
            if( ( ( abs(s1) + abs(s2) + abs(s3) + abs(s4) ) - S ) / ( abs(s1) + abs(s2) + abs(s3) + abs(s4) ) <= 1e-5 ) ///精度要高一点,原来设置为 1e-2 就在第22个测试点WA了

                printf( "YES\n" );
            else
                printf( "NO\n" );
        }
    }
    return 0;
}
图1 精度设置较低导致WA

       题目2:A. In Search of an Easy Problem——https://codeforces.com/contest/1030/problem/A

       AC代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, num;
    bool flag;
    while( ~scanf( "%d", &n ) )
    {
        flag = true;
        for( int i = 0; i < n; i ++ )
        {
            scanf( "%d", &num );
            if(num)
            flag = false;
        }
        printf( "%s\n", flag ? "EASY" : "HARD" );
    }
    return 0;
}

       题目3:https://vjudge.net/contest/65959#problem/A

       AC代码:

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>


using namespace std;

const int maxn = 1e3 + 3;
int cnt, tot, n, k;
int vis[maxn];
char Str[maxn][maxn];

void dfs(int);

int main()
{
    while( ~scanf( "%d%d", &n, &k ) && n != -1 && k != -1 )
    {
        cnt = tot = 0;
        memset( vis, 0, sizeof(vis) );
        /*
        if( n == -1 && k == -1 )
            break;
        */
        for( int i = 1; i <= n; i ++ )
            //scanf( "%s", Str[i] );
            //cin >> Str[i];
            for( int j = 1; j <= n; j ++ )
                cin >> Str[i][j];
                //scanf( "%c", &Str[i][j] );
        dfs(1);
        printf( "%d\n", tot );
    }
    return 0;
}

void dfs( int cur )
{
    if( cnt == k )
    {
        tot ++;
        return;
    }
    if( cur == n + 1 )
        return;
    for( int i = 1; i <= n; i ++ )
    {
        if( Str[cur][i] == '#' && !vis[i] )
        {
            cnt ++;
            vis[i] = 1;
            dfs( cur + 1 );
            cnt --;
            vis[i] = 0;
        }
    }
    dfs( cur + 1 );
}

       题目5:https://vjudge.net/contest/65959#problem/B

       AC代码:

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <malloc.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>

using namespace std;

const int maxn = 33;
char Map[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int l, r, c;
bool flag = false;

struct node
{
    int x, y, z, cnt;
    node( int a, int b, int c, int d )
    {

        x = a;
        y = b;
        z = c;
        cnt = d;

        //x = a, y = b, z = c, cnt = d;
    }
};

void dfs( int, int, int );

int main()
{
    while( ~scanf( "%d%d%d", &l, &r, &c ) && l && c && r )
    {
        for( int i = 0; i < l; i ++ )
        {
            getchar();
            for( int j = 0; j < r; j ++ )
            {
                fill( vis[i][j], vis[i][j] + c + 1, false );
                gets(Map[i][j]);
                //cin >> Map[i][j];
            }
        }

        for( int i = 0; i < l; i ++ )
        {
            for( int j = 0; j < r; j ++ )
            {
                for( int k = 0; k < c; k ++ )
                {
                    if( Map[i][j][k] == 'S' )
                    {
                        flag = false;
                        dfs( i, j, k );
                        break;
                    }
                }
            }
        }

        /*
        for( int i = 0; i < l; i ++ )
        {
            for( int j = 0; j < r; j ++ )
            {
                for( int k = 0; k < c; k ++ )
                {
                    cout<<Map[i][j][k];
                }
                cout << endl;
            }
        }
        */
        if( !flag )
            printf( "Trapped!\n" );
    }
    return 0;
}

void dfs( int x, int y, int z )
{
    queue<node>q;
    node start( x, y, z, 0 );
    q.push(start);
    while( !q.empty() )
    {
        node p = q.front();
        q.pop();
        if( p.x < l && p.x >= 0 && p.y >= 0 && p.y < r && p.z >= 0 && p.z < c )
        {
            if( Map[p.x][p.y][p.z] == 'E' )
            {
                flag = true;
                printf( "Escaped in %d minute(s).\n", p.cnt );
                break;
            }
            else if( Map[p.x][p.y][p.z] != '#' && !vis[p.x][p.y][p.z] )
            {
                vis[p.x][p.y][p.z] = true;
                {
                    node tmp( p.x + 1, p.y, p.z, p.cnt + 1 );
                    q.push(tmp);
                }
                {
                    node tmp( p.x - 1, p.y, p.z, p.cnt + 1 );
                    q.push(tmp);
                }
                {
                    node tmp( p.x, p.y + 1, p.z, p.cnt + 1 );
                    q.push(tmp);
                }
                {
                    node tmp( p.x, p.y - 1, p.z, p.cnt + 1 );
                    q.push(tmp);
                }
                {
                    node tmp( p.x, p.y, p.z + 1, p.cnt + 1 );
                    q.push(tmp);
                }
                {
                    node tmp( p.x, p.y, p.z - 1, p.cnt + 1 );
                    q.push(tmp);
                }
            }
        }
    }
}

       题目6:https://vjudge.net/contest/65959#problem/C

       AC代码:

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
//#include <malloc.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>

using namespace std;

const int maxn = 1e6 + 13;

int dp[maxn];
bool vis[maxn];
int n, k;

int bfs( int, int );

int main()
{
    while( ~scanf( "%d%d", &n, &k ) )
    {
        memset( vis, false, sizeof(vis) );
        vis[n] = true;
        /*
        说法①:
        就是入队的都是出现过的
        n已经出现过了,不再入队了
        说法②:
        初始的点要标记已经走过,不然会重复经过
        如果n=k,那么ans会变成2而不是0
        */
        memset( dp, 0, sizeof(dp) );
        printf( "%d\n", bfs( n, k ) );
    }
    return 0;
}

int bfs( int n, int m )
{
    queue<int>q;
    q.push(n);
    int ans;
    while( !q.empty() )
    {
        int tmp = q.front();
        q.pop();
        for( int i = 1; i <= 3; i ++ )
        {
            if( i == 1 )
                ans = tmp - 1;
            else if( i == 2 )
                ans = tmp + 1;
            else
                ans = 2 * tmp;
            if( ans < 0 || ans > maxn )
                continue;
            if( !vis[ans] )
            {
                vis[ans] = true;
                dp[ans] = dp[tmp] + 1;
                q.push(ans);
            }
        }
    }
    return dp[k];
}

猜你喜欢

转载自blog.csdn.net/u25th_engineer/article/details/82828213