栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1162 The Same Game

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1162 The Same Game

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<stdio.h> using namespace std;enum {    ROW = 10,    COL = 15,};int mark[COL][ROW];char game[COL][ROW];int ans;int mv[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};bool valid(int r, int c){    return (r>=0&&r<ROW)&&(c>=0&&c<COL);}int test(int idx, int r, int c){    char color = game[c][r];    int ret = 1;    mark[c][r] = idx;    queue<int> q;    q.push((r<<16)|c);    int nr, nc;    while(!q.empty()){        c = q.front(); q.pop();        r = c>>16; c&=0xffff;        for (int i=0; i<4; ++i){ nr = r + mv[i][0]; nc = c + mv[i][1]; if (valid(nr,nc) && game[nc][nr]==color && mark[nc][nr]==0){     mark[nc][nr] = idx;     q.push((nr<<16)|nc);     ++ret; }        }    }    return ret;}bool good(int &row, int &col, int &sidx){    int r, c, t, idx=1;    ans = 0;    memset(mark, 0, sizeof(mark));    for (c=0;game[c][0]; ++c){        for (r=0; r<ROW&&game[c][r]; ++r){ if (mark[c][r]) continue; t = test(idx, r, c); if (t>=2 && t > ans){     ans = t;     row = r;     col = c;     sidx = idx; } ++idx;        }    }    return ans;}void fill(int row, int col, int idx){    int r, c, i, j;    for (c=col; game[c][0]; ++c){        j = 0;        for (i=0; i<ROW; ++i){ if (mark[c][i] == idx) continue; game[c][j++] = game[c][i];        }        while (j<ROW){ game[c][j++] = 0;        }    }    j=col;    for (i=col; i<COL; ++i){        if (game[i][0]==0) continue;        memmove(game[j++], game[i], sizeof(game[0]));    }    while(j<COL){        if (game[j][0]){ memset(game[j], 0, sizeof(game[0]));        }        ++j;    }}void fun(){    int r, c;    int score = 0;    int t = 1;    int left = ROW * COL;    int idx;    while(good(r, c, idx)){        int pnt = (ans-2)*(ans-2);        score += pnt;        printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.n",      t++, r+1, c+1, ans, game[c][r], pnt);        fill(r, c, idx);        left -= ans;    }    if (left == 0){        score += 1000;    }    printf("Final score: %d, with %d balls remaining.n", score, left);}void readIn(){    int r, c;    char ch;    for (r=ROW-1; r>=0; --r){        for (c=0; c<COL; ++c){ scanf(" %c", &ch); game[c][r] = ch;        }    }}int main(){    int bn, gn, tn;    scanf("%d", &bn);    while(bn-->0){        scanf("%d", &gn);        tn = 1;        while(gn-- > 0){ printf("Game %d:nn", tn++); readIn(); fun(); if(gn) printf("n");        }        if (bn) printf("n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号