编程介的小学生 2019-05-30 11:13 采纳率: 20.5%
浏览 722

判断第二个矩阵是不是第一个矩阵的子矩阵,怎么用C语言的程序的代码的编写的思想的方式去实现的程序

Problem Description
There is a matrix of size n*n whose elements are either 0 or 1. Now your task is to find out that given a matrix of size m*m whose elements are also 0 or 1 whether it is a sub-matrix of the previous matrix.

Input
There is a matrix of size n*n whose elements are either 0 or 1. Now your task is to find out that given a matrix of size m*m whose elements are also 0 or 1 whether it is a sub-matrix of the previous matrix.

Output
If the second matrix is a sub-matrix of the first one, print “Yes” on a single line. Otherwise print “No”.

Sample Input
4 2
0 0 0 1
0 1 0 1
1 0 1 0
0 0 0 0
1 0
0 0
3 2
0 0 1
0 1 0
1 0 0
0 0
1 1

Sample Output
Yes
No

  • 写回答

1条回答 默认 最新

  • I__like__c 2021-12-05 20:12
    关注

    #include<stdio.h>
    int main()
    {
    int a[10][10],b[10][10],N,M,x=-1,y,flag,i,j;
    scanf("%d",&N);
    for(int i=0; i<N; i++)
    {
    for(int j=0; j<N; j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    scanf("%d",&M);
    for(int i=0; i<M; i++)
    {
    for(int j=0; j<M; j++)
    {
    scanf("%d",&b[i][j]);
    }
    }
    for(i=0; i<=N-M; i++)
    {
    for(j=0; j<=N-M; j++)
    {
    if(a[i][j]==b[0][0])
    {
    flag=1;
    for(int k=0; k<M; k++)
    {
    for(int u=0; u<M; u++)
    {
    if(a[i+k][j+u]!=b[k][u])
    flag=-1;
    }
    }
    if(flag==1)
    {
    y=i;
    x=j;
    }
    break;
    }
    }
    }
    if(x!=-1) printf("%d,%d",y+1,x+1);

    else printf("-1");
    return 0;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败