设为首页 加入收藏

TOP

NYOJ---蛇形填数(方块填数+三角填数)(一)
2015-07-24 06:51:38 来源: 作者: 【 】 浏览:87
Tags:NYOJ--- 蛇形 方块 三角

蛇形填数

时间限制:3000 ms | 内存限制:65535 KB 难度:3
描述
在n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
输入
直接输入方陈的维数,即n的值。(n<=100)
输出
输出结果是蛇形方陈。
样例输入
3
样例输出
7 8 1
6 9 2
5 4 3
来源

算法经典

解析:个人认为是道比较经典的模拟题,很考察基本功,今天又拿来做了一下,方法就是模拟每个方向的坐标变化即可,顺便推荐本书,感觉刘汝佳的《算法竞赛入门经典》很适合入门的人来看哈!

贴一下自己的代码

#include 
      
       
using std::endl;
using std::cin;
using std::cout;
const int MAXN = 100 +10;
int data[MAXN][MAXN];
int main()
{
#ifdef LOCAL
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w" , stdout);
#endif
	int n;
	while(cin >> n)
	{
		memset(data,0,sizeof(data));
		int x=0 , y = n-1 , cnt = 1;
		data[x][y] = cnt;
		while(cnt < n*n)
		{
			//向下走
			while(x+1
       
        =0 && !data[x][y-1]) { data[x][--y] = ++cnt; } //向上走 while(x-1 >=0 && !data[x-1][y]) { data[--x][y] = ++cnt; } //向右走 while(y+1 
        
         

蛇形填数(二)

时间限制:2000 ms | 内存限制:65535 KB 难度:3
描述
1  2  3  4  5
12 13 14 6
11 15 7
10 8
9
跟蛇形填数一样,只是填数要求按照三角形填。注意每组数据之间用空行隔开
输入
第一行有一个N,表示N组测试数据
接下来每组数据包括一个数字X,表示三角形的边长,0< X <1000
输出
输出之后填好之后的图
样例输入
2
5
4
样例输出
1  2  3  4  5
12 13 14 6
11 15 7
10 8
9

1  2  3  4
9  10 5
8  6
7
来源
NBOJ

解析:蛇形填数的变形,三角填数,本质还是模拟数字填充的方法即可哈!

贴一下自己的代码

#include 
              
               
#include 
               
                 using std::endl; using std::cin; using std::cout; const int MAXN = 1000 + 10; int data[MAXN][MAXN]; int main() { int n; #ifdef LOCAL freopen("input.txt" , "r" , stdin); freopen("output.txt" , "w" , stdout); #endif cin >> n; while(n--) { memset(data , 0 , sizeof(data)); int length; cin >> length; int x=0 , y=0 , cnt = 1; data[x][y] = cnt; while(cnt < ((length+1)*length)/2) { //向右走 while(y+1 < length-x && !data[x][y+1]) { data[x][++y] = ++cnt; } //向对角线走 while(x+1 < length && y-1>=0 && !data[x+1][y-1]) { data[++x][--y] = ++cnt; } //向上走 while(x-1>=0 && !data[x-1][y]) { data[--x][y] = ++cnt; } } //输出 for(int i=0; i
                
                 

<script type="text/java script">
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
您对本文章有什么意见或着疑问吗?请到 论坛讨论您的关注和建议是我们前行的参考和动力??
上一篇: Design Pattern Singleton 单一模式
下一篇: C++Primer学习笔记《1》
相关文章
<script type="text/java script">BAIDU_CLB_fillSlot("182716");
<script type="text/java script">BAIDU_CLB_fillSlot("517916");
图文推荐
<script> (function(){ var appid = 'cyrBEfE7C', conf = 'prod_830794cf494da8b808afb2994cfe0fee'; var doc = document, s = doc.createElement('script'), h = doc.getElementsByTagName('head')[0] || doc.head || doc.documentElement; s.type = 'text/java script'; s.charset = 'utf-8'; s.src = 'http://assets.changyan.sohu.com/upload/changyan.js?conf='+ conf +'&appid=' + appid; h.insertBefore(s,h.firstChild); win
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++Primer学习笔记《1》 下一篇HDU1342 Lotto [深搜]

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: