F - Buggy Sat

Discovery Co. ltd. builds a satellite using a new kind of an intelligent camera. The camera has special software to detect cities and roads from an image, and is also able to detect every region (which is a connected part of surface), bounded by a series of connected roads with no other region inside. Using this technology, the satellite is able to compress the picture before sending it. The compressed format of a picture is the city locations and its regions.
Discovery has launched the satellite, without testing the software completely. So, after a while, they received some buggy pictures which includes one more extra region: the outer region. The outer region is the region of the plane enclosing every other region (which has infinite area). Further analysis shows that all images sent have the following properties:
1. All cities, in the image, have at least two roads to the other cities.
2. There is a path connecting every pair of cities.
3. There is at most one road between each pair of cities.
4. Roads do not cross each other except at the cities.
这里写图片描述
The above Figure shows a sample image received (see sample input).
You are to write a program to read a buggy image and report the outer region.

Input
The first line of the input consists of a single integer N (1 ≤ N ≤ 20), which is the number of test cases. The test cases appear with no blank lines in between. The first line of each test case consists of the number of cities (between 1 and 50) followed by pairs of integers (x, y) which are location of cities (each pair in one line), followed by number of faces in a separate line (between 1 and 50), followed by face information on each line. Face information consists of number of cities making the face and the city numbers in clockwise (or counterclockwise) order.
Output
There should be a single line containing the boundary face number for each test case, with no blank lines in between.
Sample Input
1
5
2 6
4 4
4 7
8 6
4 10
3
4 1 2 4 3
4 1 3 4 5
4 1 2 4 5
Sample Output
3

题意:给你一幅图,和若干个分块,问哪一个才有可能是原来的那个分块。(原来是只有一个分块的,然后这个东西出bug了,然后就多了几块)

其实就是求最大面积的那块。
多边形面积可以分成多个三角形,三角形面积是两边向量相乘*(1/2),要注意点在内部,往内凹的情况。

代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
struct Point
{
    int x;
    int y;
}point[55];
struct Line
{
    int x;
    int y;
}line1,line2;
int work(Line a,Line b)
{
    return (a.x*b.y-a.y*b.x);
    //可能会有负值,就是凹进去的情况
}
void solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&point[i].x,&point[i].y);
    }
    int m;
    cin>>m;
    int b[55][550];
    int cnt[55];
    for(int i=0;i<m;i++)
    {
        cin>>cnt[i];
        for(int j=0;j<cnt[i];j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    int smax=0;
    int id=0;
    for(int i=0;i<m;i++)
    {
        int s=0;
        for(int j=1;j<cnt[i]-1;j++)
        {
            line1.x=point[b[i][0]].x-point[b[i][(j)]].x;
            line1.y=point[b[i][0]].y-point[b[i][(j)]].y;
            line2.x=point[b[i][0]].x-point[b[i][(j+1)]].x;
            line2.y=point[b[i][0]].y-point[b[i][(j+1)]].y;
            s+=work(line1,line2);
        }
        s=abs(s);//在最后取正值比较
        if(s>smax)
        {
            id=i+1;
            smax=s;
        }
    }
    printf("%d\n",id);
}
int main (void)
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
以下是 Moon-Buggy 的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <curses.h> #include <time.h> #define VERSION "1.0" #define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define WIDTH 64 #define HEIGHT 16 #define PLAYER_POS_X 4 #define PLAYER_POS_Y (HEIGHT-3) #define DISTANCE_PER_FRAME 8 #define OBSTACLE_PERIOD 8 #define JUMP_DURATION 12 #define JUMP_HEIGHT 3 #define GRAVITY 1 #define SPRITE_GAP 2 static char player_up[] = {'O',0,'-','^',0,'|',0,0,'|',0,'^',0,'-',0,'O',0}; static char player_down[] = {'O',0,'-','v',0,'|',0,0,'|',0,'v',0,'-',0,'O',0}; static char obstacle1[] = {'*',' ',' ',0,'*','*',0,'*',' ',0,'*','*',0,'*',' ',0,'*',0}; static char obstacle2[] = {'*',0,0,'*',' ',0,'*','*',0,'*','*',0,'*',' ',0,'*',0,0}; static char *obstacles[] = {obstacle1, obstacle2}; static int sprite_widths[] = {16, 17}; static int obstacle_probabilities[] = {60, 40}; static int obstacle_y[] = {HEIGHT-3, HEIGHT-4}; static char *title[] = { "", " Moon Buggy", "", "", "", " Press SPACE to start", " Press Q to quit", "", " Version " VERSION, "", "", "", "", "", "", NULL }; static char *gameover[] = { "", "", "", " GAME OVER", "", "", "", " Press SPACE to restart", " Press Q to quit", "", "", "", "", "", "", NULL }; static char *score_prefix[] = { "SCORE: ", NULL }; static char score[16]; static int obstacle_pos = WIDTH-1; static int obstacle_type = 0; static int obstacle_speed = 1; static int obstacle_visible = 0; static int player_pos_y = PLAYER_POS_Y; static int player_speed_y = 0; static int player_jump_remaining = 0; static int distance = 0; static int score_length; static int frame_count = 0; static int frame_rate = 20; static int frame_period = 1000/frame_rate; static int screen_width = WIDTH+1; static int screen_height = HEIGHT+4; static WINDOW *win; static void draw_sprite(int x, int y, char *sprite, int width) { int i, j; for (i = 0; i < width; i++) { if (sprite[i]) { mvwaddch(win, y, x+i, sprite[i]); } } } static void draw_title() { int i; int y = (screen_height - sizeof(title)/sizeof(title[0])) / 2; for (i = 0; title[i]; i++) { mvwaddstr(win, y+i, (screen_width-strlen(title[i]))/2, title[i]); } } static void draw_gameover() { int i; int y = (screen_height - sizeof(gameover)/sizeof(gameover[0])) / 2; for (i = 0; gameover[i]; i++) { mvwaddstr(win, y+i, (screen_width-strlen(gameover[i]))/2, gameover[i]); } } static void draw_score() { sprintf(score, "%d", distance); mvwaddstr(win, 0, screen_width-score_length-1, score_prefix[0]); wattron(win, A_REVERSE); mvwaddstr(win, 0, screen_width-score_length-1+strlen(score_prefix[0]), score); wattroff(win, A_REVERSE); } static void draw_player() { if (player_jump_remaining) { draw_sprite(PLAYER_POS_X, player_pos_y, player_up, sprite_widths[0]); } else { draw_sprite(PLAYER_POS_X, player_pos_y, player_down, sprite_widths[1]); } } static void draw_obstacle() { if (obstacle_visible) { draw_sprite(obstacle_pos, obstacle_y[obstacle_type], obstacles[obstacle_type], sprite_widths[obstacle_type]); } } static void draw() { werase(win); draw_score(); draw_player(); draw_obstacle(); wrefresh(win); } static void update_player() { if (player_jump_remaining) { player_pos_y -= player_speed_y; player_speed_y -= GRAVITY; if (player_pos_y >= PLAYER_POS_Y) { player_jump_remaining = 0; player_pos_y = PLAYER_POS_Y; player_speed_y = 0; } } } static void update_obstacle() { if (++frame_count % OBSTACLE_PERIOD == 0) { if (!obstacle_visible) { obstacle_visible = 1; obstacle_type = rand() % 2; obstacle_pos = WIDTH - sprite_widths[obstacle_type]; obstacle_speed = 1 + rand() % 3; } } if (obstacle_visible) { obstacle_pos -= obstacle_speed; if (obstacle_pos < -sprite_widths[obstacle_type]) { obstacle_visible = 0; } } } static void update_distance() { distance += DISTANCE_PER_FRAME; score_length = strlen(score_prefix[0]) + strlen(score); } static void handle_input() { int c = getch(); if (c == ' ') { if (player_pos_y == PLAYER_POS_Y) { player_jump_remaining = JUMP_DURATION; player_speed_y = JUMP_HEIGHT; } else if (!obstacle_visible) { distance = 0; } } else if (c == 'q') { endwin(); exit(0); } } static void init() { initscr(); cbreak(); noecho(); curs_set(0); nodelay(stdscr, TRUE); win = newwin(screen_height, screen_width, 0, 0); srand(time(NULL)); } static void cleanup() { delwin(win); endwin(); } static void game_loop() { for (;;) { update_player(); update_obstacle(); update_distance(); draw(); handle_input(); if (player_pos_y == PLAYER_POS_Y && obstacle_visible && obstacle_type == 0 && obstacle_pos == PLAYER_POS_X && obstacle_y[0] == PLAYER_POS_Y) { draw_gameover(); wrefresh(win); while (getch() != ' ') { /* wait for space */ } distance = 0; obstacle_visible = 0; player_pos_y = PLAYER_POS_Y; player_speed_y = 0; player_jump_remaining = 0; frame_count = 0; } napms(frame_period); } } int main(int argc, char *argv[]) { init(); draw_title(); wrefresh(win); while (getch() != ' ') { /* wait for space */ } game_loop(); cleanup(); return 0; } ``` 这个代码是一个使用 curses 库的 Moon-Buggy 游戏。游戏中,玩家需要控制一辆月球车,躲避障碍物并尽可能地前进,直到撞上障碍物或按下 Q 键退出游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值