简单模拟 UVa 10267 - Graphical Editor

来源:互联网 发布:linux rm删除文件夹 编辑:程序博客网 时间:2024/05/27 20:38

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1208

题目意思:

     给一连串命令,对矩阵进行操作。

解题思路:

     注意画横线和竖线时,题目只说在两端点之间,因此要判断大小。

     填充矩形时注意坐标端点大小比较。

     创建图形时注意行和列的位置,哪在前哪在后。

     找出某点的关联点用DFS。

总结:不要想当然的认为满足一定的条件,坐标大小顺序。注意DFS的简洁高效。

代码:

#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<map>#define eps 1e-6#define INF (1<<20)#define PI acos(-1.0)using namespace std;char mapp[260][260]; //map定义为全局变量时有冲突int flag[260][260];int col,row;bool judge(int x,int y){    if(x>=1&&x<=row&&y>=1&&y<=col)        return true;    return false;}void dfs(int x,int y,char temp,char stan){    //printf("%c %d %d\n",temp,col,row);wz    mapp[x][y]=stan;    flag[x][y]=1;    //上下左右四个方向的更新    if(judge(x,y-1)&&mapp[x][y-1]==temp&&flag[x][y-1]==0)        dfs(x,y-1,temp,stan);    if(judge(x,y+1)&&(mapp[x][y+1]==temp)&&flag[x][y+1]==0)        dfs(x,y+1,temp,stan);    if(judge(x-1,y)&&mapp[x-1][y]==temp&&flag[x-1][y]==0)        dfs(x-1,y,temp,stan);    if(judge(x+1,y)&&mapp[x+1][y]==temp&&flag[x+1][y]==0)        dfs(x+1,y,temp,stan);    return ;}int main(){    char order[3],temp[3];    while(scanf("%s",order)!=EOF)    {        if(*order=='X')            break;        switch(*order)        {            case 'I':scanf("%d%d",&col,&row); //注意不要把这写反                     for(int i=1;i<=row;i++)                        for(int j=1;j<=col;j++)                            mapp[i][j]='O';                    break;            case 'C':for(int i=1;i<=row;i++)                        for(int j=1;j<=col;j++)                            mapp[i][j]='O';                    break;            case 'L':int a,b;                    scanf("%d%d%s",&a,&b,temp);                    mapp[b][a]=*temp;                    break;            case 'V':int tempcol,startrow,endrow;                     scanf("%d%d%d%s",&tempcol,&startrow,&endrow,temp);                    if(startrow>endrow)                        swap(startrow,endrow); //题目只是说两行之间                     for(int i=startrow;i<=endrow;i++)                        mapp[i][tempcol]=*temp;                     break;            case 'H':int startcol,endcol,temprow;                     scanf("%d%d%d%s",&startcol,&endcol,&temprow,temp);                     if(startcol>endcol)                        swap(startcol,endcol);                     for(int i=startcol;i<=endcol;i++)                        mapp[temprow][i]=*temp;                     break;            case 'K':int leupx,leupy,ridox,ridoy;                     scanf("%d%d%d%d%s",&leupy,&leupx,&ridoy,&ridox,temp);                     if(leupx>ridox)                        swap(leupx,ridox);                     if(leupy>ridoy)                        swap(leupy,ridoy);                     for(int i=leupx;i<=ridox;i++)                        for(int j=leupy;j<=ridoy;j++)                          mapp[i][j]=*temp;                     break;            case 'F':int x,y;                     scanf("%d%d%s",&y,&x,temp);                     memset(flag,0,sizeof(flag));                     dfs(x,y,mapp[x][y],*temp);                     break;            case 'S':char name[1000];                     scanf("%s",name);                     printf("%s\n",name);                     for(int i=1;i<=row;i++)                     {                         mapp[i][col+1]='\0';                         printf("%s\n",mapp[i]+1);                     }                     break;            default :char atemp[10000];                    gets(atemp); //略去不是命令的        }    }    return 0;}