2015 ICPC 北京场E、F、G (计算几何)

E - Cats and Fish

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 105;
int c[N];
int tmp[N];
int main()
{
    int m, n, x;
    while(~scanf("%d %d %d", &m, &n, &x)){
        for(int i = 1; i <= n; i++){
            scanf("%d", &c[i]);
            tmp[i] = 0;
        }
        sort(c + 1, c+ n+1);
        int p = m, q = 0;
        for(int i = 1; i <= x; i++){
            for(int j = 1; j <= n; j++){
                if(tmp[j] == 0){
                    if(p > 0){
                        p--;
                        tmp[j] = c[j];
                    }
                }
                tmp[j]--;

            }
        }
        for(int i = 1; i <= n; i++){
            if(tmp[i] > 0) q++;
        }
        printf("%d %d\n", p, q);
    }

}

F - Secret Poems

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 110;
char save[maxn][maxn];
char out[maxn*maxn];
char res[maxn][maxn];
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
bool vis[maxn][maxn];
int main(){
    int n,i,j;
    while(~scanf("%d", &n)){
        for(i=0;i<n;i++){
            scanf("%s",save[i]);
        }
        int x = 0;
        int y = 0;
        bool up = true;
        for(i=0;i<n*n;i++){
            out[i] = save[x][y];
            if(up){
                if(x-1<0 || y+1>=n){
                    if(y == n-1){
                        x += 1;
                    }else{
                        y += 1;
                    }
                    up = !up;
                }else{
                    x -= 1;
                    y += 1;
                }
            }else{
                if(y-1<0 || x+1>=n){
                    if(x == n-1){
                        y += 1;
                    }else{
                        x += 1;
                    }
                    up = !up;
                }else{
                    x += 1;
                    y -= 1;
                }
            }
        }
        int dir = 0;
        x = 0, y = 0;
        memset(vis, false, sizeof(vis));
        for(i=0;i<n*n;i++){
            res[x][y] = out[i];
            vis[x][y] = true;
            if(x + dx[dir]<0 || x + dx[dir] >=n || y+dy[dir]>=n || y+dy[dir]<0 || vis[x+dx[dir]][y+dy[dir]]){
                dir = (dir + 1)%4;
            }
            x += dx[dir];
            y += dy[dir];
        }
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                printf("%c", res[i][j]);
            }
            printf("\n");
        }
    }

    return 0;
}

G - Liaoning Ship’s Voyage(计算几何)

题意是很简单的最短路,但是在整点的地图上存在着端点非整数点的三角形。在向终点走的时候要看路径是否穿过三角区域。把当前路径看成一个线段。分路径和三角形三边有几个交点来讨论的。
几种情况
若路径线段和三角形边界重合,直接ok,否则:
一个交点:仅当当前路径线段端点都不在三角形区域内才可通行
两个交点:当三角形端点就是交点且路径端点都不在三角形区域内时才可通行
三个交点:直接不可通行

把三角形做凸多边形的时候其实还要把三个点按照顺时针排序的。。。。并没有排。。。。留到计算几何以后学吧。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 50;
const int inf = 0x3f3f3f3f;
const double eps = 1e-10;
int dx[8] = {0,0,1,-1,1,1,-1,-1};
int dy[8] = {1,-1,0,0,1,-1,1,-1};
int n;
int cmp(double x){
    if(fabs(x)<eps){
        return 0;
    }
    if(x>0) return 1;
    return -1;
}
inline double sqr(double x){
    return x*x;
}
int sgn(double x){
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    else return 1;
}
struct point{
    double x,y;
    point() {}
    point(double a, double b): x(a), y(b){}
    friend point operator + (const point &a, const point &b){
        return point(a.x + b.x, a.y + b.y);
    }
    friend point operator - (const point &a, const point &b){
        return point(a.x - b.x, a.y - b.y);
    }
    friend bool operator == (const point &a, const point &b){
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend point operator * (const double &a, const point &b){
        return point(a * b.x, a * b.y);
    }
    friend point operator / (const point &a, const double &b){
        return point(a.x / b, a.y / b);
    }
    double operator ^ (const point &b){
        return x * b.y - y * b.x;
    }
    double norm(){
        return sqrt(sqr(x) + sqr(y));
    }

};
point apo[4];
double det(const point &a, const point &b){
    return a.x * b.y - a.y * b.x;
}
double dot(const point &a, const point &b){
    return a.x * b.x + a.y * b.y;
}

struct line{
    point a,b;
    line(){}
    line(point x, point y) : a(x), b(y){}
};
line point_make_line(const point a, const point b){
    return line(a, b);
}
bool PointOnSegment(point p, point s, point t){
    return cmp(det(p-s, t-s)) == 0 && cmp(dot(p-s, p-t)) <= 0;
}
bool parallel(line a, line b) {
    return !cmp(det(a.a - a.b, b.a - b.b));
}
bool lineCover(line a, line b){
    if(parallel(a,b)){
        if(PointOnSegment(a.a, b.a, b.b) || PointOnSegment(a.b, b.a, b.b)
           || PointOnSegment(b.a, a.a, a.b) || PointOnSegment(b.b, a.a, a.b)){
            return true;
        }
    }
    return false;
}
int Point_In(point t){
    int num = 0, i, d1, d2, k;
    apo[3] = apo[0];
    for(int i = 0; i < 3; i++){
        if(PointOnSegment(t,apo[i],apo[i+1])) return 2;
        k = cmp(det(apo[i+1]-apo[i],t-apo[i]));
        d1 = cmp(apo[i].y-t.y);
        d2 = cmp(apo[i+1].y-t.y);
        if(k>0 && d1 <= 0 && d2 > 0) num++;
        if(k < 0 && d2 <= 0 && d1 > 0) num--;
    }
    return num!=0;

}
bool line_make_point(line a, line b, point &res){

    if(parallel(a,b)) return false;
    bool flag =
    max(a.a.x, a.b.x) >= min(b.a.x, b.b.x) &&
    max(b.a.x, b.b.x) >= min(a.a.x, a.b.x) &&
    max(a.a.y, a.b.y) >= min(b.a.y, b.b.y) &&
    max(b.a.y, b.b.y) >= min(a.a.y, a.b.y) &&
    sgn((b.a - a.b) ^ (a.a - a.b)) * sgn((b.b - a.b) ^ (a.a - a.b)) <= 0 &&
    sgn((a.a - b.b) ^ (b.a - b.b)) * sgn((a.b - b.b) ^ (b.a - b.b)) <= 0;
    if(flag){
        double s1 = det(a.a - b.a, b.b - b.a);
        double s2 = det(a.b - b.a, b.b - b.a);

        res = (s1 * a.b - s2 * a.a) / (s1 - s2);
        return true;
    }else{
        return false;
    }
}

char save[maxn][maxn];
char killme[maxn][maxn];
char in[maxn];
int dis[maxn][maxn];
line Tline[3];
bool notDangerous(point s, point e){
    if(Point_In(e)==1 || Point_In(s) == 1) return false;
    line now =  line(s,e);
    int i, sum = 0;
    point madePoint[3];
    int index[3], p = -1;
    for(i=0;i<3;i++){
        if(lineCover(Tline[i], now)){
            return true;
        }
        if(line_make_point(now, Tline[i], madePoint[i])){
            index[++p] = i;
            sum++;
        }
    }
    if(sum>1){
        point tlp, ntp;
        if(sum==2){
            line_make_point(Tline[index[0]], Tline[index[1]], tlp);
            line_make_point(Tline[index[0]], now, ntp);
            if(ntp == tlp){
                if(Point_In(e)==1 || Point_In(s)==1){
                    return false;
                }
                return true;
            }else{
                return false;
            }
        }
        return false;
    }else if(sum==1){
        if(Point_In(e) == 2 || Point_In(s) == 2){
            return true;
        }else{
            return false;
        }
    }
    return true;
}

int bfs(){
    int i,j;
    queue<pair<int, int> > que;
    memset(dis, inf, sizeof(dis));
    pair<int,int> s,e,q,next;
    int nx, ny;
    s.first = 0, s.first = 0;
    e.first = n-1, e.second = n-1;
    que.push(s);
    dis[0][0] = 0;
    while(!que.empty()){
        q = que.front();
        que.pop();
        if(q == e){
            return dis[n-1][n-1];
        }
        for(i=0;i<8;i++){
            nx = q.first + dx[i];
            ny = q.second + dy[i];
            if(0<=nx&&nx<n && 0<=ny&&ny<n && dis[nx][ny] > dis[q.first][q.second]+1 && save[nx][ny]!= '#'){
                if(!notDangerous(point((double)q.first, (double)q.second), point((double)nx, (double)ny))) continue;
                next.first = nx;
                next.second = ny;
                dis[nx][ny] = dis[q.first][q.second] + 1;
                que.push(next);
            }
        }
    }
    return -1;
}

int main(){
    int i,j,len;
    double x1,y1,x2,y2,x3,y3;
    while(~scanf("%d",&n)){
        scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
        apo[0].x = x1, apo[0].y = y1;
        apo[1].x = x2, apo[1].y = y2;
        apo[2].x = x3, apo[2].y = y3;
        for(i=0;i<n;i++){
            scanf("%s", killme[i]);
        }
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                save[j][n-1-i] = killme[i][j];
            }
        }
        point p1 = point(x1,y1);
        point p2 = point(x2,y2);
        point p3 = point(x3,y3);
        Tline[0].a = p1, Tline[0].b = p2;
        Tline[1].a = p1, Tline[1].b = p3;
        Tline[2].a = p2, Tline[2].b = p3;
        point test;
        printf("%d\n", bfs());
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值