心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
以下是我的代码:
#include<stdio.h>
#include
<string.h>
const long maxn=257,xd[]={-1,0,1,0},yd[]={0,1,0,-1};
char g[maxn][maxn];
bool used[maxn][maxn];
long m,n;
void I(char *s)
{
    sscanf(s,
"%ld%ld",&m,&n);
    
for(long i=1;i<=n;i++)
      
for(long j=1;j<=m;j++)
        g[i][j]
='O';
}
void C()
{
    
for(long i=1;i<=n;i++)
      
for(long j=1;j<=m;j++)
        g[i][j]
='O';
}
void L(char *s)
{
    
long x,y;
    
char tmp;
    sscanf(s,
"%ld%ld %c",&x,&y,&tmp);
    g[y][x]
=tmp;
}
void V(char *s)
{
    
long x,y1,y2;
    
char tmp;
    sscanf(s,
"%ld%ld%ld %c",&x,&y1,&y2,&tmp);
    
if(y1>y2)
    {
       
long t=y1;y1=y2;y2=t;
    }
    
for(long i=y1;i<=y2;i++)
      g[i][x]
=tmp;
}
void H(char *s)
{
    
long x1,x2,y;
    
char tmp;
    sscanf(s,
"%ld%ld%ld %c",&x1,&x2,&y,&tmp);
    
if(x1>x2)
    {
       
long t=x1;x1=x2;x2=t;
    }
    
for(long i=x1;i<=x2;i++)
      g[y][i]
=tmp;
}
void K(char *s)
{
    
long x1,y1,x2,y2;
    
char tmp;
    sscanf(s,
"%ld%ld%ld%ld %c",&x1,&y1,&x2,&y2,&tmp);
    
if(x1>x2)
    {
       
long t=x1;x1=x2;x2=t;
    }
    
if(y1>y2)
    {
       
long t=y1;y1=y2;y2=t;
    }
    
for(long i=y1;i<=y2;i++)
      
for(long j=x1;j<=x2;j++)
        g[i][j]
=tmp;
}
void floodfill(long x,long y,char color)
{
    
for(long k=0;k<4;k++)
    {
       
long a=x+xd[k],b=y+yd[k];
       
if(a>=1&&a<=n&&b>=1&&b<=m&&g[x][y]==g[a][b]&&!used[a][b])
       {
          used[a][b]
=true;
          floodfill(a,b,color);
       }
    }
    g[x][y]
=color;
}
void F(char *s)
{
    
long x,y;
    
char tmp;
    sscanf(s,
"%ld%ld %c",&x,&y,&tmp);
    
for(long i=1;i<=n;i++)
      
for(long j=1;j<=m;j++)
        used[i][j]
=false;
    used[y][x]
=true;
    floodfill(y,x,tmp);
}
void S(char *s)
{
    
char name[maxn];
    sscanf(s,
"%s",name);
    printf(
"%s\n",name);
    
for(long i=1;i<=n;i++)
    {
       
for(long j=1;j<=m;j++)
         printf(
"%c",g[i][j]);
       putchar(
'\n');
    }
}


int main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
char cmd[maxn];
    
while(gets(cmd)!=NULL)
    {
       
switch(cmd[0])
       {
          
case 'I':
               I(cmd
+2);break;
          
case 'C':
               C();
break;
          
case 'L':
               L(cmd
+2);break;
          
case 'V':
               V(cmd
+2);break;
          
case 'H':
               H(cmd
+2);break;
          
case 'K':
               K(cmd
+2);break;
          
case 'F':
               F(cmd
+2);break;
          
case 'S':
               S(cmd
+2);break;
          
case 'X':
               
return 0;
       }
    }
return 0;
}


posted on 2010-01-24 17:39 lee1r 阅读(753) 评论(2)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟

FeedBack:
# re: UVa 10267 Graphical Editor
2010-02-01 09:52 | YanHH
sscanf
格式老是不会输入,学到新东西了
Thank you!  回复  更多评论
  
# re: UVa 10267 Graphical Editor
2010-02-01 10:37 | YanHH
UVa give me fouth Runtime Error!
Can you help me?

#include <iostream>
using namespace std;

char cmd[100], tmp;
char board[251][251];
int col, row;
char color, tmpColor;

void Go(int x, int y)
{
board[y][x] = color;
if(y > 1 && board[y-1][x] == tmpColor) Go(x, y-1);
if(y < row && board[y+1][x] == tmpColor) Go(x, y+1);
if(x > 1 && board[y][x-1] == tmpColor) Go(x-1, y);
if(x < col && board[y][x+1] == tmpColor) Go(x+1, y);
}

int main()
{
while(true) {
gets(cmd);
if('X' == cmd[0]) break;
if('I' == cmd[0]) {
sscanf(cmd, "%c%d%d", &tmp, &col, &row);
for(int i = 1; i <= row; i++) {
for(int j = 1; j <= col; j++)
board[i][j] = 'O';
}
} else if('C' == cmd[0]) {
for(int i = 1; i <= row; i++) {
for(int j = 1; j <= col; j++)
board[i][j] = 'O';
}
} else if('L' == cmd[0]) {
int x, y;
sscanf(cmd, "%c%d%d %c", &tmp, &x, &y, &color);
board[y][x] = color;
} else if('V' == cmd[0]) {
int x, y1, y2;
sscanf(cmd, "%c%d%d%d %c", &tmp, &x, &y1, &y2, &color);
for(int i = y1; i <= y2; i++) {
board[i][x] = color;
}
} else if('H' == cmd[0]) {
int x1, x2, y;
sscanf(cmd, "%c%d%d%d %c", &tmp, &x1, &x2, &y, &color);
for(int i = x1; i <= x2; i++) {
board[y][i] = color;
}
} else if('K' == cmd[0]) {
int x1, x2, y1, y2;
sscanf(cmd, "%c%d%d%d%d %c", &tmp, &x1, &y1, &x2, &y2, &color);
for(int i = y1; i <= y2; i++) {
for(int j = x1; j <= x2; j++)
board[i][j] = color;
}
} else if('F' == cmd[0]) {
int x, y;
sscanf(cmd, "%c%d%d %c", &tmp, &x, &y, &color);
tmpColor = board[y][x];
Go(x, y);
} else if('S' == cmd[0]) {
char name[100];
sscanf(cmd, "%c%s", &tmp, name);
printf("%s\n", name);
for(int i = 1; i <= row; i++) {
for(int j = 1; j <= col; j++)
printf("%c", board[i][j]);
printf("\n");
}
}
}
return 0;
}  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理