pascal问题(与二维数组有关)

问题描述:

pascal问题(与二维数组有关)
1、蛇形排数
(snake.pas/c/cpp)
【问题描述】
同学A遇到一个难题,要求是输入一个整数N,输出一个N行N列的方阵,方阵为蛇形数字组合.他想请你帮忙解决一下这个难题.
【输入文件】snake.in
一个整数,N.(2≤N≤20)
【输出文件】snake.out
N行N列,为输出的蛇形数字组合.各数字域宽为4.
【样例输入】
4
【样例输出】
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
2、设计程序,输出所有的数独形式(9*9)
注:数字相同,但方向不同,认为是不同形式
答出悬赏50
1个回答 分类:综合 2014-10-29

问题解答:

我来补答
const go:array[1..4,1..2]of longint=((0,1),(1,0),(0,-1),(-1,0));
var n,i,j:longint;
map:array[0..25,0..25]of longint;
used:array[0..25,0..25]of boolean;
procedure dfs(x,y,k,d:longint);
begin
if used[x,y] then exit;
used[x,y]:=true;
map[x,y]:=k;
if used[x+go[d,1],y+go[d,2]] then inc(d);
if d=5 then d:=1;
dfs(x+go[d,1],y+go[d,2],k+1,d)
end;
begin
readln(n);
fillchar(used,sizeof(used),true);
for i:=1 to n do
for j:=1 to n do used[i,j]:=false;
dfs(1,n,1,2);
for i:=1 to n do
begin
for j:=1 to n-1 do write(map[i,j],' ');
writeln(map[i,n]);
end;
end.
再问: 请问数独那个怎么做?
再答: 我果断搜索了,跑了2分钟了,还没出结果 我还是把我的代码放上吧,有点秃 const biao:array[1..9,1..9]of longint=((1,1,1,2,2,2,3,3,3),(1,1,1,2,2,2,3,3,3), (1,1,1,2,2,2,3,3,3),(4,4,4,5,5,5,6,6,6),(4,4,4,5,5,5,6,6,6), (4,4,4,5,5,5,6,6,6),(7,7,7,8,8,8,9,9,9),(7,7,7,8,8,8,9,9,9), (7,7,7,8,8,8,9,9,9)); var a,b,c:array[1..9,1..9]of boolean; map:array[1..9,1..9]of longint; procedure print; var i,j:longint; begin for i:=1 to 9 do begin for j:=1 to 8 do write(map[i,j],' '); writeln(map[i,9]); end; end; procedure dfs(x,y:longint); var i:longint; begin if y=10 then begin inc(x); y:=1; end; if x=10 then print; for i:=1 to 9 do if (not a[x,i])and(not b[y,i])and(not c[biao[x,y],i]) then begin a[x,i]:=true; b[y,i]:=true; c[biao[x,y],i]:=true; map[x,y]:=i; dfs(x,y+1); a[x,i]:=false; b[y,i]:=false; c[biao[x,y],i]:=false; end; end; begin fillchar(a,sizeof(a),false); fillchar(b,sizeof(b),false); fillchar(c,sizeof(c),false); dfs(1,1); end.
再问: 我测试程序只输出了一组
再答: 我的确实输出了N多组,我看着都很对
 
 
展开全文阅读
剩余:2000
上一页:求形状的题不会