| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2327 人关注过本帖
标题:图形问题求解
只看楼主 加入收藏
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:13 
图形问题求解
Problem Description
Given the gray value of each transformation point of the image in n row m column. After the conversion is performed successively,  please print the final image.
The possible operations and corresponding characters are as follows:
A: Rotate clockwise by 90 degrees;
B: Rotate counterclockwise by 90 degrees ;
C: Flip left and right;
D: Turn upside down.

Input
Multi testcases(<=10).
For each testcase:
    two integers n, m (1 <= n,m <= 100) in the first line
    m integers (0 <= a[i][j] <= 255) in next n lines
    a string s (|s| <= 100) in a line, means the operations.
Output
For each testcase:
    m integers in n lines meas the final image
Sample Input
2 3
10 0 10
100 100 10
AC

Sample Output
10 100
0 100
10 10
搜索更多相关主题的帖子: line Output the image 图形 
2020-11-08 16:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
你哪里不会?我觉得这题关键是压缩s(我预估最终只有那种可能),而不是每一部都去做一下
2020-11-08 19:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下代码是我随手瞎敲的,尤其是第一个switch中的公式,我根本没验证过它们(对我而言,思路是正确的就行了),仅供参考

程序代码:
#include <stdio.h>

int main( void )
{
    for( unsigned n,m; scanf("%u%u",&n,&m)==2; )
    {
        unsigned char a[100*100];
        char s[100+1];
        for( unsigned i=0; i!=n*m; ++i )
            scanf( "%hhu", a+i );
        scanf( " %s", s );

        unsigned mode = 0;
        for( const char* p=s; *p; ++p )
        {
            switch( *p )
            {
            case 'A': mode=mode/4*4 + (mode+3)%4; break;
            case 'B': mode=mode/4*4 + (mode+1)%4; break;
            case 'C': mode=(mode+4)%8; break;
            case 'D': mode=mode%4/2*4-2; break;
            }
        }

        size_t first, step1, step2, line;
        switch( mode )
        {
        case 0: first=0;     step1=1;       step2=0;     line=m; break;
        case 1: first=m-1;   step1=m;       step2=0;     line=n; break;
        case 2: first=n*m-1; step1=n*m;     step2=0;     line=m; break;
        case 3: first=n*m-m; step1=n*m-m+1; step2=0;     line=n; break;
        case 4: first=m-1;   step1=n*m;     step2=2*m;   line=m; break;
        case 5: first=n*m-1; step1=n*m-m+1; step2=n*m-1; line=n; break;
        case 6: first=n*m-m; step1=1;       step2=2*n*m+2-2*m;   line=m; break;
        case 7: first=0;     step1=m;       step2=2;     line=n; break;
        }
        for( size_t i=0; i!=n*m; ++i )
            printf( "%hhu%c", a[(first+i*step1+i/line*step2)%(n*m+1)], " \n"[(i+1)%line==0] );
    }
}



[此贴子已经被作者于2020-11-10 12:54编辑过]

2020-11-09 13:58
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
感觉无从下手 这个转换
2020-11-09 16:23
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
回复 3楼 rjsp
哥 你能解释下代码的意义吗 我看不太懂 而且我指针不是很熟练
2020-11-09 16:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 5楼 xiaohuo66
代码不重要,你可以按照自己希望的方式写出来
重要的是你要能看出来,无论那个s有多长,有多复杂,最终只有8种可能的结果。把这个结果算出来,那就可以免掉所有的旋转和翻转。
2020-11-09 17:04
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
回复 6楼 rjsp
请问为啥八种啊? 而且算出来是什么意思 我以为旋转交换是建一个二维数组然后变换数组的位置
2020-11-09 17:11
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
回复 6楼 rjsp
我好像知道啥意思了,但您这代码我还是看不懂
2020-11-09 17:45
xiaohuo66
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2020-11-1
收藏
得分:0 
  unsigned mode = 0;
        for( const char* p=s; *p; ++p )
        {
            switch( *p )
            {
            case 'A': mode=mode/4*4 + (mode+3)%4; break;
            case 'B': mode=mode/4*4 + (mode+1)%4; break;
            case 'C': mode=(mode+4)%8; break;
            case 'D': mode=mode%4/2*4-2; break;
            }
        }
能讲下这是啥意思吗? 麻烦大佬了
2020-11-09 17:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
我大体已经理解,但我很感兴趣您是怎么巧妙的使无论s多长使得相同结果输出同样的mode,并且怎样把八种case用一个表达式printf的呢。


“顺时针旋转90度,然后再水平翻转” 是不是就等同于 “从左到右,从下到上 打印矩阵”?
任意多个连续操作,最终都得到8个可能的结果之一。

我加了点注释,并且将 压缩s 的方式改为了查表,这样能看懂吗?
程序代码:
#include <stdio.h>

int main( void )
{
    for( unsigned r,c; scanf("%u%u",&r,&c)==2; )
    {
        unsigned char a[100*100];
        char s[100+1];
        for( unsigned i=0; i!=r*c; ++i )
            scanf( "%hhu", a+i );
        scanf( " %s", s );

        static const unsigned char transmap[8][8] =
                    { { 0, 1, 2, 3, 4, 5, 6, 7 }
                    , { 1, 2, 3, 0, 5, 6, 7, 4 }
                    , { 2, 3, 0, 1, 6, 7, 4, 5 }
                    , { 3, 0, 1, 2, 7, 4, 5, 6 }
                    , { 4, 7, 6, 5, 0, 3, 2, 1 }
                    , { 5, 4, 7, 6, 1, 0, 3, 2 }
                    , { 6, 5, 4, 7, 2, 1, 0, 3 }
                    , { 7, 6, 5, 4, 3, 2, 1, 0 } };
        unsigned mode = 0;
        for( const char* p=s; *p; ++p )
        {
            switch( *p )
            {
            case 'A': mode=transmap[mode][3]; break;
            case 'B': mode=transmap[mode][1]; break;
            case 'C': mode=transmap[mode][4]; break;
            case 'D': mode=transmap[mode][6]; break;
            }
        }

        size_t first, step1, step2, line;
         switch( mode )
        {
        case 0: first=0;     step1=1;       step2=0;           line=c; break; // 从上到下,从左到右。(逆时针旋转0度)(没有操作)
        case 1: first=c-1;   step1=c;       step2=0;           line=r; break; // 从右到左,从上到下。(逆时针旋转90度)
        case 2: first=r*c-1; step1=r*c;     step2=0;           line=c; break; // 从下到上,从右到左。(逆时针旋转180度)
        case 3: first=r*c-c; step1=r*c-c+1; step2=0;           line=r; break; // 从左到右,从下到上。(逆时针旋转270度)
        case 4: first=c-1;   step1=r*c;     step2=2*c;         line=c; break; // 从上到下,从右到左。(逆时针旋转0度,再左右颠倒)
        case 5: first=r*c-1; step1=r*c-c+1; step2=r*c-1;       line=r; break; // 从右到左,从下到上。(逆时针旋转90度,再左右颠倒)
        case 6: first=r*c-c; step1=1;       step2=2*r*c+2-2*c; line=c; break; // 从下到上,从左到右。(逆时针旋转180度,再左右颠倒)(上下颠倒)
        case 7: first=0;     step1=c;       step2=2;           line=r; break; // 从左到右,从上到下。(逆时针旋转270度,再左右颠倒)
        }

        for( size_t i=0; i!=r*c; ++i )
            printf( "%hhu%c", a[(first+i*step1+i/line*step2)%(r*c+1)], " \n"[(i+1)%line==0] );
    }
}
2020-11-10 13:11
快速回复:图形问题求解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021761 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved