• 牛客练习赛12 AB


    A 圆圆

    链接:https://www.nowcoder.net/acm/contest/68/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”。
    示例1

    输入

    3
    3.14 3.13
    -3.14 -3.13
    1.00 2.00

    输出

    clockwise
    counterclockwise
    counterclockwise

    备注:

    1≤T≤105
    −π≤tA,tB
    A≠B
    输入中的浮点数精确至小数点下两位
     
    需要注意PI,因为保留两位小数,一开始把PI当成3.14算一直出错,多加几位就过了。
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    
    int main() {
    	int t;
    	cin >> t ;
    	double pi = 3.1415926535;
    	while(t--) {
    		double a, b;
    		cin >> a >> b;
    		if(a > b) {
    			if(a-b < b+2*pi-a) {
    				cout << "clockwise
    ";
    			}else cout << "counterclockwise
    ";
    		}else {
    			if(a+2*pi-b < b-a)  {
    				cout << "clockwise
    ";
    			}else cout << "counterclockwise
    ";
    		}
    	}
    	return 0;
    }
    

     

    B 迷宫

    链接:https://www.nowcoder.net/acm/contest/68/B
    来源:牛客网

    题目描述

    这是一个关于二维迷宫的题目。我们要从迷宫的起点 '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'


    最短路径,只是多了钥匙和门,由于都只出现一次,那样就只有两种情况了,一种是直接从S->E,还有一种是从S->K->D->E
    这样就要算了。

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int N = 550;
    char str[N][N];
    int vis[N][N], xx[4], yy[4], ans;
    int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
    struct Nod {
    	int x, y, step;
    }pos, q;
    
    int bfs(int x, int y, int gx, int gy) {
    	queue<Nod> que;
    	pos.x = x, pos.y = y;
    	pos.step = 0;
    	que.push(pos);
    	vis[x][y] = 1;
    	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 nx = q.x + dx[i], ny = q.y + dy[i];
    			if(str[nx][ny] != 'W' && vis[nx][ny] == 0 && str[nx][ny] == '.' ) {
    				pos.x = nx, pos.y = ny;
    				pos.step = q.step + 1;
    				que.push(pos);
    				vis[nx][ny] = 1;
    			}
    		}
    	}
    	return -1;
    }
    void init(){
    	for(int i = 0; i < N; i ++) {
    		for(int j = 0; j < N; j ++) {
    			vis[i][j] = 0;
    		}
    	}
    }
    int main() {
    	int n, m;
    	cin >> n >> m;
    	for(int i = 1; i <= n; i ++) cin >> str[i]+1;
    	for(int i = 1; i <= n; i ++) {
    		for(int j = 1; j <= m; j ++) {
    			if(str[i][j] == 'S') {
    				xx[0] = i; yy[0] = j;
    			} 
    			if(str[i][j] == 'E') {
    				xx[1] = i; yy[1] = j;
    			}
    			if(str[i][j] == 'K') {
    				xx[2] = i; yy[2] = j;
    			}
    			if(str[i][j] == 'D') {
    				xx[3] = i; yy[3] = j;
    			}
    		}
    	}
    	str[xx[0]][yy[0]] = str[xx[2]][yy[2]] = str[xx[1]][yy[1]] = '.';
    	init();
    	int ans0 = bfs(xx[0],yy[0],xx[1],yy[1]);
    	init();
    	int ans1 = bfs(xx[0],yy[0],xx[2],yy[2]);
    	init();
    	str[xx[3]][yy[3]] = '.';
    	int ans2 = bfs(xx[2],yy[2],xx[3],yy[3]);
    	init();
    	int ans3 = bfs(xx[3],yy[3],xx[1],yy[1]);
    	int ans4 = -1;
    	if(ans0 != -1) ans4 = ans0;
    	if(ans1 != -1 && ans2 != -1 && ans3 != -1) {
    		if(ans0 == -1) ans4 = ans1+ans2+ans3;
    		else ans4 = min(ans4, ans1+ans2+ans3);
    	}
    	
    	printf("%d
    ",ans4);
    	return 0;
    }
    

      

  • 相关阅读:
    TOYS POJ
    口罩发放
    Qin Shi Huang's National Road System HDU
    次小生成树
    ACM Contest and Blackout UVA
    Strongly connected HDU
    tarjan
    Network HDU
    【洛谷 1351】联合权值
    【洛谷 3884】二叉树问题
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8436862.html
Copyright © 2020-2023  润新知