我有以下代码:
#include <stdio.h>
int main() {
int tc,T;
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i,j]);
}
}
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i,j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}
这给了我以下错误:
你知道这段代码有什么问题吗?
P.S我还检查了网格和模式,它读取垃圾!
答案 0 :(得分:3)
在C中你应该下标这样的数组:array[i][j]
array[i, j]
相当于array[j]
,因为i
会被忽略。
答案 1 :(得分:1)
c中的数组不像Grid[i, j]
那样被访问,而是被Grid[i][j]
访问。所以将其更改为代码中的内容。同样,Pattern[i, j]
应写为Pattern[i][j]
。
这将解决您的问题。
乐意帮助! :)
答案 2 :(得分:0)
尝试以下内容:
#include <stdio.h>
int main() {
int tc,T;
printf("Enter one number\n");
scanf("%d", &T);
for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
printf("Enter 2 numbers\n");
scanf("%d %d", &R, &C);
{
int i,j;
int r,c;
int Grid[R][C];
printf("now reading the grid\n");
//Read the Grid
for(i=0; i<R; i++){
for(j=0; j<C; j++){
scanf("%d", &Grid[i][j]);
}
}
printf("Enter 2 mre numbers\n");
scanf("%d %d", &r, &c);
{
int Pattern[r][c];
printf("now reading the pattern\n");
//Read the Grid
for(i=0; i<r; i++){
for(j=0; j<c; j++){
scanf("%d", &Pattern[i][j]);
}
}
//Here we have both the Grid and the Pattern
for(i=0; i<R; i++){
for(j=0; j<C; j++){
if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){
int x,y;
int innerFlag=1;
for(x=0; x<r; x++){
for(y=0; y<c; y++){
if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0;
}
}
if(innerFlag == 1){
//Set the flag to 1 and break out of the nested loops
flag=1;
j=C;
i=R;
}
}
}
}
//Here all the calculation is done and the flag is set
if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}