跳到正文

s2014_07_10

1314156

我要AK!!!(awa)

在洛谷打开
资料、奖项与版本

资料

编号
1314156
抓取

陶片放逐

− 发送私信

查看记录

创世沈

C++
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;

int x,y;
int hp=5;
int score=0;
int rx,ry;
int mx,my;
int mhp=6;
bool monLive=1;
char maze[30][60];

void gotoxy(int x,int y)
{
    COORD p={x,y};
    HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(h,p);
}

void buildmap()
{
    for(int i=0;i<25;i++)
    {
        for(int j=0;j<50;j++)
        {
            if(i==0||i==24||j==0||j==49)maze[i][j]='#';
            else maze[i][j]=' ';
        }
    }
    for(int i=0;i<90;i++)
    {
        int a=1+rand()%23;
        int b=1+rand()%48;
        maze[a][b]='#';
    }
}

void drawmap()
{
    for(int i=0;i<25;i++)
    {
        for(int j=0;j<50;j++)
        {
            gotoxy(j,i);
            cout<<maze[i][j];
        }
    }
}

void newstar()
{
    while(1)
    {
        int tx=2+rand()%40;
        int ty=2+rand()%20;
        if(maze[ty][tx]==' ')
        {
            rx=tx;ry=ty;
            break;
        }
    }
}

void newmon()
{
    while(1)
    {
        int tx=6+rand()%34;
        int ty=6+rand()%14;
        if(maze[ty][tx]==' '&&abs(tx-x)>8&&abs(ty-y)>8)
        {
            mx=tx;my=ty;
            break;
        }
    }
}

void monmove()
{
    if(!monLive)return;
    int nx=mx,ny=my;
    int dx=x-mx;
    int dy=y-my;
    int rd=rand()%5;
    if(rd==0&&dx>0&&maze[my][mx+1]==' ')nx++;
    else if(rd==1&&dx<0&&maze[my][mx-1]==' ')nx--;
    else if(rd==2&&dy>0&&maze[my+1][mx]==' ')ny++;
    else if(rd==3&&dy<0&&maze[my-1][mx]==' ')ny--;
    gotoxy(mx,my);
    cout<<" ";
    mx=nx;my=ny;
}

int main()
{
    HANDLE hc=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ci;
    GetConsoleCursorInfo(hc,&ci);
    ci.bVisible=0;
    SetConsoleCursorInfo(hc,&ci);
    srand((unsigned int)time(0));

    buildmap();
    drawmap();
    x=10;y=10;
    newstar();
    newmon();
    monLive=1;
    mhp=6;
    int movecnt=0;
    bool fightcd=0;
    int cdtime=0;

    while(1)
    {
        gotoxy(0,0);
        cout<<"分数:"<<score<<" 生命:"<<hp<<" 怪物血量:"<<mhp<<"      ";
        gotoxy(x,y);
        cout<<" ";

        if(_kbhit())
        {
            char k=_getch();
            int nx=x,ny=y;
            if(k=='w')ny--;
            if(k=='s')ny++;
            if(k=='a')nx--;
            if(k=='d')nx++;
            if(k=='j'&&!fightcd&&monLive)
            {
                if(abs(x-mx)<=2&&abs(y-my)<=2)
                {
                    mhp--;
                    fightcd=1;
                    cdtime=15;
                }
            }
            if(maze[ny][nx]==' ')
            {
                x=nx;y=ny;
            }
        }

        if(fightcd)
        {
            cdtime--;
            if(cdtime<=0)fightcd=0;
        }

        movecnt++;
        if(movecnt>=6)
        {
            monmove();
            movecnt=0;
        }

        if(monLive)
        {
            gotoxy(mx,my);
            cout<<"●";
        }
        gotoxy(x,y);
        cout<<"○";
        gotoxy(rx,ry);
        cout<<"★";

        if(rx==x&&ry==y)
        {
            score++;
            gotoxy(rx,ry);
            cout<<" ";
            newstar();
        }

        if(monLive&&mx==x&&my==y)
        {
            hp--;
            gotoxy(x,y);
            cout<<" ";
            while(1)
            {
                int tx=8+rand()%25;
                int ty=8+rand()%12;
                if(maze[ty][tx]==' ')
                {
                    x=tx;y=ty;
                    break;
                }
            }
        }

        if(mhp<=0&&monLive)
        {
            monLive=0;
            score+=80;
            gotoxy(18,10);
            cout<<"击杀成功+80分";
            Sleep(1200);
            gotoxy(18,10);
            cout<<"              ";
            gotoxy(mx,my);
            cout<<" ";
            newmon();
            mhp=6;
            monLive=1;
        }

        if(hp<=0)
        {
            gotoxy(18,10);
            cout<<"游戏结束 总分:"<<score;
            Sleep(2500);
            exit(0);
        }
        Sleep(70);
    }
    return 0;
}
//来源:创世沈
//awa
共 1 篇
  1. 题解

    题解:P16736 [GKS 2019 #E] Code-Eat Switcher

    该题要求判断是否存在一种时段分配策略,使得总编码量A和总饭量B同时满足给定的查询条件。核心思路是通过数学转化将问题转化为资源分配的优化模型,并利用贪心算法和前缀和实现高效判定。 关键步骤: 问题转化:将饭量约束转化为吃饭占比的编码消耗限制,即在总编码A固定时,需确保饭量B满足条件。 排序策略:按单位饭量消耗的编码比值C_i/E_i升序排序,优先选择吃饭效率高的时段。 前缀和预处理:计算排序后的时段编码和饭量前缀和,用于快速查询最大可获得饭量。 二分查找:通过upper_bound确定能完全分配的时段数量,剩余编码按比例分配给下一个时段以最大化饭量。 精度处理:使用1e-8的容差避免浮点数误差导致的误判。 判定逻辑: 若A>总编码量或B>总饭量,直接返回N。 计算吃饭编码消耗上限limC=sumC-A,通过贪心策略在limC限制下求最大饭量maxE。 若maxE≥B(考虑精度误差),则存在可行方案,返回Y;否则返回N。 代码特点: 时间复杂度O(S log S + (S+D) log S),适用于S,D≤1e5的数据规模。 使用long long避免溢出,通过cin.tie(nullptr)和ios::sync_with_stdio(false)加速输入。 修正了原题解的逻辑错误,确保在资源限制下正确判断是否存在满足条件的分配方案。