调试耗时——旋转矩阵

旋转矩阵一

时间限制:1000MS  内存限制:65535K

Description

    某天,tianjiewang在搭火车,由于他搭车后容易晕车,突然他想到了矩阵的旋转。
给一个3*3的矩阵,矩阵旋转是按顺时针的
例如

           1 2 3          7 4 1
           4 5 6 转一次是 8 5 2 
           7 8 9          9 6 3

           1 2 3          9 8 7
           4 5 6 转两次是 6 5 4
           7 8 9          3 2 1
问给一个n*m 的矩阵,问顺时针旋转k次后,求旋转后的矩阵是?(1<=n,m,k<=100),由于正在搭火车,tianjiewang的头很晕,
不知怎样做,现在他向你求助,你可以帮助他吗?

(出题人tianjiewang)

输入格式

第一行输入 n,m,k;
第二行到第n + 1行,每行有m 个数,每个数的取值范围是(1<=Aij<=100)

输出格式

输出n*m矩阵顺时针旋转k次后的矩阵

输入样例

3 3 1
1 2 3
4 5 6 
7 8 9

输出样例

7 4 1
8 5 2
9 6 3

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cmath>
//#include <cctype>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
//#include <sstream>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;

#define MP make_pair
#define PB push_back
#define mst(a,b) memset((a),(b),sizeof(a))
#define TEST cout<<"*************************"<<endl

#define rep(s,n,up) for(int i = (s); i < (n); i+=(up))
#define per(n,e,down) for(int i = (n); i >= (e); i-=(down))
#define rep1(s,n,up) for(int j = (s); j < (n); j+=(up))
#define per1(n,e,down) for(int j = (n); j >= (e); j-=(down))

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const double Pi = acos(-1.0);
const int maxn = (1 << 16) + 7;
const uLL Hashmod = 29050993;
const double esp=1e-6;


//#define local

int main() {
#ifdef local
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    //ios::sync_with_stdio(0);
    //cin.tie();
    int a[100][100];
    mst(a,0);
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int mod=k%4;
    /*switch(mod){
        case(0):{
            int x,y,tot,s=n*m;
            a[x=0][y=0]=tot=1;
            while(tot<s){
            while(y+1<m&&!a[x][y+1])a[x][++y]=++tot;
            while(x+1<n&&!a[x+1][y])a[++x][y]=++tot;
            while(y-1>=0&&!a[x][y-1])a[x][--y]=++tot;
            while(x-1>=0&&!a[x-1][y])a[--x][y]=++tot;}
            for(int i=0;i<n;++i)
            {
                for(int j=0;j<m;++j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
        case(1):{
            int x,y,tot,s=n*m;
            a[x=0][y=n-1]=tot=1;
            while(tot<s){
            while(x+1<m&&!a[x+1][y])a[++x][y]=++tot;
            while(y-1>=0&&!a[x][y-1])a[x][--y]=++tot;
            while(x-1>=0&&!a[x-1][y])a[--x][y]=++tot;
            while(y+1<n&&!a[x][y+1])a[x][++y]=++tot;}
            for(int i=0;i<m;++i)
            {
                for(int j=0;j<n;++j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
        case(2):{
            int x,y,tot,s=n*m;
            a[x=n-1][y=m-1]=tot=1;
            while(tot<s){
            while(y-1>=0&&!a[x][y-1])a[x][--y]=++tot;
            while(x-1>=0&&!a[x-1][y])a[--x][y]=++tot;
            while(y+1<m&&!a[x][y+1])a[x][++y]=++tot;
            while(x+1<n&&!a[x+1][y])a[++x][y]=++tot;}
            for(int i=0;i<n;++i)
            {
                for(int j=0;j<m;++j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
        case(3):{
            int x,y,tot,s=n*m;
            a[x=m-1][y=0]=tot=1;
            while(tot<s){
            while(x-1>=0&&!a[x-1][y])a[--x][y]=++tot;
            while(y+1<n&&!a[x][y+1])a[x][++y]=++tot;
            while(x+1<m&&!a[x+1][y])a[++x][y]=++tot;
            while(y-1>=0&&!a[x][y-1])a[x][--y]=++tot;}
            for(int i=0;i<m;++i)
            {
                for(int j=0;j<n;++j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
    }*/
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
            scanf("%d",&a[i][j]);
    }
    switch(mod){
        case(0):{
            for(int i=0;i<n;++i)
            {
                for(int j=0;j<m;++j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
        case(1):{
            for(int i=0;i<m;++i)
            {
                for(int j=n-1;j>=0;--j)
                    printf("%d ",a[j][i]);
                printf("\n");
            }
            break;
        }
        case(2):{
            for(int i=n-1;i>=0;--i)
            {
                for(int j=m-1;j>=0;--j)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
            break;
        }
        case(3):{
            for(int i=m-1;i>=0;--i)
            {
                for(int j=0;j<n;++j)
                    printf("%d ",a[j][i]);
                printf("\n");
            }
            break;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值