牛客网练习赛12---A and B

A题传送门:https://www.nowcoder.net/acm/contest/68/A

B题传送门:   https://www.nowcoder.net/acm/contest/68/B

A题:

题目描述

我们定义一个圆 C 为以原点 (0, 0) 为中心的单位圆(半径为 1 的圆)。给定在 C 圆周上相异的两点

A, B。请问由 A 出发,沿着圆周走到 B,是顺时针走比较近,还是逆时针走比较近呢?

C 的圆周上的所有点都可以用 (cos(t), sin(t)) 来表示,其中 t 的物理意义为角度。也就是说,在圆 C 中,给定一角度 t 即可确定在圆周上的一点。在这题中,所有的角度皆以弧度制表示,另外,由于不同的t 值有机会对应到同一个圆周上的点,我们限制t 的范围为[-π,π )。

本题中,我们会用tA 以及tB 来代表点A 及点B,数学上,A = (cos(tA), sin(tA)), B = (cos( tB), sin(tB))。

输入描述:

输入的第一行有一个正整数T,代表接下来共有几组测试数据。

接下来的T行,每行有两个浮点数tA, tB,代表一组数据。

输出描述:

对于每组数据请输出一行,如顺时针比较近请输出“clockwise”,否则请输出“counterclockwise”。

输入

3
3.14 3.13
-3.14 -3.13
1.00 2.00

输出

clockwise
counterclockwise
counterclockwise

备注:

1≤T≤105
−π≤tA,tB
A≠B
输入中的浮点数精确至小数点下两位
 
思路:因为是以原点为圆心的单位圆,所以可以直接用类似凸包里面的叉乘的极角排序,判断线段OA和线段OB的相对关系。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<assert.h>
using namespace std;
double pi=acos(-1.0);
struct point{
    double x,y;
};
double cross(double x1,double y1,double x2,double y2){
    return (x1*y2-x2*y1);
}
double cmp(point a,point b,point c){
    return cross((b.x - a.x),(b.y - a.y),(c.x - a.x),(c.y-a.y));
}
int main(){
    int t;
    double a,b;
    for(scanf("%d",&t);t--;){
        cin>>a>>b;
        point pa,pb,pc;
        pa.x = cos(a); pa.y = sin(a);
        pb.x = cos(b); pb.y = sin(b);
        pc.x = 0.00  ; pc.y = 0.00;
        double k = cmp(pa,pc,pb);
        //printf("%f
",k);
        if(k > 0) puts("clockwise");
        else      puts("counterclockwise");
    }
}
View Code

B题:

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

这是一个关于二维迷宫的题目。我们要从迷宫的起点 'S' 走到终点 'E',每一步我们只能选择上下左右四个方向中的一个前进一格。 'W' 代表墙壁,是不能进入的位置,除了墙壁以外的地方都可以走。迷宫内的 'D' 代表一道上锁的门,只有在持有钥匙的时候才能进入。而 'K' 则代表了钥匙,只要进入这一格,就会自动地拿到钥匙。最后 '.' 则是代表空无一物的地方,欢迎自在的游荡。

本题的迷宫中,起点、终点、门跟钥匙这四个特殊物件,每一个恰好会出现一次。而且,此迷宫的四周 (最上面的一行、最下面的一行、最左边的一列以及最右边的一列) 都会是墙壁。

请问,从起点到终点,最少要走几步呢?

输入描述:

输入的第一行有两个正整数H, W,分别代表迷宫的长跟宽。
接下来的H行代表迷宫,每行有一个长度恰为W的字串,此字串只包含`'S'`, `'E'`, `'W'`, `'D '`, `'K'`, `'.'`这几种字元。

输出描述:

请在一行中输出一个整数代表答案,如果无法从起点走到终点,请输出-1。
示例1

输入

4 12
WWWWWWWWWWWW
WE.W.S..W.KW
W..D..W....W
WWWWWWWWWWWW

输出

20
示例2

输入

6 6
WWWWWW
WEWS.W
W.WK.W
W.WD.W
W.W..W
WWWWWW

输出

-1

备注:

4 ≤ H, W≤ 500
'S', 'E', 'K', 'D'各出现恰好一次
迷宫的四周(最上面的一行、最下面的一行、最左边的一列以及最右边的一列) 都会是 'W'

思路:无非两种情况:1、起点直接到终点;2、起点到钥匙,然后钥匙到门,门再到终点。
以上面的思路跑BFS就行啦(吐槽一下自己因为ans初始值没给一直wa83.3%)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
char ma[501][501];
int vis[501][501],n,m;
int go[8][8] = {{1,0},{0,-1},{-1,0}, {0,1}};
struct note{
    int x,y,step;
}pos,q;
int bfs(int x,int y,int gx,int gy){
    queue<note>que;
    vis[x][y] = 1;
    q.x = x;q.y=y;q.step = 0;
    que.push(q);
    while(que.size()){
        q = que.front();
        que.pop();
        if(q.x == gx&&q.y == gy) return q.step;
        for(int i = 0 ; i < 4 ; i++){
            int dx = q.x + go[i][0];
            int dy = q.y + go[i][1];
            //printf("%d %d
",go[i][0],go[i][1]);
            if(!vis[dx][dy] && ma[dx][dy] != 'W'&& ma[dx][dy] == '.'){
                pos.x = dx;pos.y = dy;pos.step = q.step+1;
                que.push(pos);
                vis[dx][dy] =1;
            }
        }
    }
    return -1;
}
void init(){
    for(int i = 0 ; i <= n ; i ++){
        for(int j = 0 ; j <= m ; j++){
            vis[i][j] = 0;
        }
    }
}
int main(){
    int sx,sy,ex,ey,ox,oy,kx,ky;
    scanf("%d %d",&n,&m);
    init();
    for(int i = 1 ; i <= n ; i++){
        scanf("%s",ma[i]);
        for(int j = 0 ; j < m ; j ++){
            if(ma[i][j] == 'S'){
                sx = i;sy = j;
            }
            if(ma[i][j] == 'E'){
                ex = i;ey = j;
            }
            if(ma[i][j] == 'D'){
                ox= i ;oy = j;
            }
            if(ma[i][j] == 'K'){
                kx = i; ky = j;
            }
        }
    }
    ma[sx][sy]=ma[ex][ey]=ma[kx][ky]='.';
    int s1 = bfs(sx,sy,ex,ey);//直接到终点
    init();
    int s2 = bfs(sx,sy,kx,ky);//去找钥匙
    init();
    ma[ox][oy] = '.';//因为有钥匙了,就可以走到这里了
    int s3 = bfs(kx,ky,ox,oy);//去找门
    init();
    int s4 = bfs(ox,oy,ex,ey);//门到终点
    int ans = -1;
    if(s2!=-1&&s3!=-1&&s4!=-1){
        ans = s2+s3+s4;
    }
    if(s1==-1&&ans==-1)puts("-1");
    else if(s1 == -1&&ans != -1)printf("%d
",ans);
    else if(s1 != -1&&ans == -1)printf("%d
",s1);
    else printf("%d
",min(s1,ans));
}
View Code
原文地址:https://www.cnblogs.com/Esquecer/p/8438755.html