<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: up code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 22:11:20 GMT</pubDate>
    <description>DZone Snippets: up code</description>
    <item>
      <title>A solution for the "Stack 'em Up" problem</title>
      <link>http://snippets.dzone.com/posts/show/5356</link>
      <description>A solution for the "Stack 'em Up" problem.&lt;br /&gt;&lt;br /&gt;Problem description:&lt;br /&gt;&lt;a href="http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10205.html"&gt;http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10205.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Author: &lt;a href="http://joanatrindade.wikidot.com"&gt;Joana Matos Fonseca da Trindade&lt;/a&gt;&lt;br /&gt;Date: 2008.04.05&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* &lt;br /&gt; * Solution for "Stack 'em Up" problem.&lt;br /&gt; * UVa ID: 10205&lt;br /&gt; */&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;#define NVALUES 13&lt;br /&gt;#define NSUITS 4&lt;br /&gt;#define NCARDS 52&lt;br /&gt;#define NSHUFFLES 100&lt;br /&gt;#define WSIZE 9&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;char values[NVALUES][WSIZE] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};&lt;br /&gt;char suits[NSUITS][WSIZE] = {"Clubs", "Diamonds", "Hearts", "Spades"};&lt;br /&gt;int shuffles[NSHUFFLES][NCARDS];&lt;br /&gt;int deck[NCARDS];&lt;br /&gt;&lt;br /&gt;/* read all dealer shuffles */&lt;br /&gt;void read_shuffles(int n_shuff) {&lt;br /&gt;	for(int i=0; i&lt;n_shuff; i++) {&lt;br /&gt;		for (int j=0; j&lt;NCARDS; j++) {&lt;br /&gt;			cin &gt;&gt; shuffles[i][j];&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* shuffle the deck with one of the known shuffles */&lt;br /&gt;void shuffle_deck(int s_id) {&lt;br /&gt;	int tmpdeck[NCARDS];&lt;br /&gt;	for (int i=0; i&lt;NCARDS; i++) {&lt;br /&gt;		tmpdeck[i] = deck[shuffles[s_id][i] - 1];&lt;br /&gt;	}&lt;br /&gt;	for (int i=0; i&lt;NCARDS;i++) {&lt;br /&gt;		deck[i] = tmpdeck[i];&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* main */&lt;br /&gt;int main (int argc, const char *argv[]) {&lt;br /&gt;	int nc; /* number of cases */&lt;br /&gt;	int ns;	/* number of shuffles */&lt;br /&gt;	int s; /* current shuffle */&lt;br /&gt;		&lt;br /&gt;	cin &gt;&gt; nc;&lt;br /&gt;		&lt;br /&gt;	for (int i=0; i&lt;nc; i++) {	&lt;br /&gt;		cin &gt;&gt; ns;&lt;br /&gt;			&lt;br /&gt;		/* initialize deck */&lt;br /&gt;		for (int p=0; p&lt;NCARDS; p++) {&lt;br /&gt;			deck[p] = p;&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		/* read list of known shuffles */&lt;br /&gt;		read_shuffles(ns);&lt;br /&gt;		&lt;br /&gt;		/* shuffle deck */&lt;br /&gt;		for (int j=0; j&lt;ns; j++) {&lt;br /&gt;			cin &gt;&gt; s;&lt;br /&gt;			shuffle_deck(s - 1);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		/* print deck */&lt;br /&gt;		for (int k=0; k&lt;NCARDS; k++) {&lt;br /&gt;			cout &lt;&lt; values[deck[k] % NVALUES] &lt;&lt; " of " &lt;&lt; suits[deck[k] / NVALUES] &lt;&lt; endl;&lt;br /&gt;		}&lt;br /&gt;		if (i &lt; (nc - 1)) {&lt;br /&gt;			cout &lt;&lt; endl;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 Apr 2008 00:29:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5356</guid>
      <author>jmftrindade (Joana M. F. da Trindade)</author>
    </item>
    <item>
      <title>Speeding up Dijkstra's Algorithm</title>
      <link>http://snippets.dzone.com/posts/show/4996</link>
      <description>Demonstrates a way of speeding up the O(n&lt;sup&gt;2&lt;/sup&gt;) version of Dijkstra's Algorithm by about 4x.&lt;br /&gt;&lt;br /&gt;Here, dist[][] is the adjacency matrix representing the graph and n is the number of nodes. d2[] is where the lengths of the shortest paths are saved.&lt;br /&gt;&lt;br /&gt;For a full explanation, see &lt;a href="http://compprog.wordpress.com/2008/01/17/speeding-up-dijkstras-algorithm-1/"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void dijkstra2(int s) {&lt;br /&gt;	int queue[GRAPHSIZE];&lt;br /&gt;	char inQueue[GRAPHSIZE];&lt;br /&gt;	int begq = 0,&lt;br /&gt;	    endq = 0;&lt;br /&gt;	int i, mini;&lt;br /&gt;	int visited[GRAPHSIZE];&lt;br /&gt;&lt;br /&gt;	for (i = 1; i &lt;= n; ++i) {&lt;br /&gt;		d2[i] = INFINITY;&lt;br /&gt;		visited[i] = 0; /* the i-th element has not yet been visited */&lt;br /&gt;		inQueue[i] = 0;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	d2[s] = 0;&lt;br /&gt;	queue[endq] = s;&lt;br /&gt;	endq = (endq + 1) % GRAPHSIZE;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	while (begq != endq) {&lt;br /&gt;		mini = queue[begq];&lt;br /&gt;		begq = (begq + 1) % GRAPHSIZE;&lt;br /&gt;		inQueue[mini] = 0;&lt;br /&gt;&lt;br /&gt;		visited[mini] = 1;&lt;br /&gt;&lt;br /&gt;		for (i = 1; i &lt;= n; ++i)&lt;br /&gt;			if (dist[mini][i])&lt;br /&gt;				if (d2[mini] + dist[mini][i] &lt; d2[i]) { &lt;br /&gt;					d2[i] = d2[mini] + dist[mini][i];&lt;br /&gt;					if (!inQueue[i]) {&lt;br /&gt;						queue[endq] = i;&lt;br /&gt;						endq = (endq + 1) % GRAPHSIZE;&lt;br /&gt;						inQueue[i] = 1;&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jan 2008 15:57:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4996</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>My handy mp3 "alarm clock" written in Ruby.</title>
      <link>http://snippets.dzone.com/posts/show/3498</link>
      <description>// Simple tool to help wake me up in the morning. Not the cleanest code, but it took no time to write. Ruby rocks!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/env ruby&lt;br /&gt;#&lt;br /&gt;# A helper to setup an alarm to slowly and leisurely wake up in the morning.&lt;br /&gt;#&lt;br /&gt;# $Id: mp3wakeup.rb 6 2007-01-31 16:02:57Z btek $&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;if ARGV.length &lt; 1&lt;br /&gt;	puts "Usage: #{$0} &lt;time&gt; [mp3dir]"&lt;br /&gt;	exit 1&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#############################&lt;br /&gt;# Configuration parameters. #&lt;br /&gt;#############################&lt;br /&gt;&lt;br /&gt;NUM_STEPS = 15&lt;br /&gt;STEP_DURATION = 60 * 1&lt;br /&gt;&lt;br /&gt;FINAL_VOLUME = 95&lt;br /&gt;INITIAL_VOLUME = 30&lt;br /&gt;VOLUME_STEP = (FINAL_VOLUME - INITIAL_VOLUME) / NUM_STEPS&lt;br /&gt;&lt;br /&gt;MIN_SEC_BEFORE_ALARM = NUM_STEPS * STEP_DURATION&lt;br /&gt;&lt;br /&gt;#############################&lt;br /&gt;#############################&lt;br /&gt;&lt;br /&gt;time = Regexp.quote ARGV[0]&lt;br /&gt;mp3dir = Regexp.quote(ARGV[1]) if ARGV.length &gt; 1&lt;br /&gt;&lt;br /&gt;def get_alarm_time(desired_hour, desired_minute)&lt;br /&gt;  now = Time.new&lt;br /&gt;  &lt;br /&gt;  alarm_time = Time.local(now.year, now.month, now.day, desired_hour, desired_minute, 0, 0)&lt;br /&gt;  &lt;br /&gt;  if alarm_time - now &lt; MIN_SEC_BEFORE_ALARM&lt;br /&gt;    alarm_time = alarm_time + 60*60*24&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  return alarm_time&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def run(cmd)&lt;br /&gt;  #puts cmd&lt;br /&gt;  system cmd&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "Waking you up by #{time} with songs from #{mp3dir}."&lt;br /&gt;&lt;br /&gt;match = time.match(/(\d+)(:(\d+))?/)&lt;br /&gt;hour = match[1].to_i&lt;br /&gt;minute = if (match.length &gt; 1)&lt;br /&gt;           match[3].to_i &lt;br /&gt;         else&lt;br /&gt;           0&lt;br /&gt;         end&lt;br /&gt;&lt;br /&gt;alarm_time = get_alarm_time(hour, minute)&lt;br /&gt;&lt;br /&gt;for i in 0..NUM_STEPS&lt;br /&gt;  alert_time = alarm_time - (STEP_DURATION * i)&lt;br /&gt;  volume = FINAL_VOLUME - (VOLUME_STEP * i)&lt;br /&gt;  &lt;br /&gt;  at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"&lt;br /&gt;  run "echo amixer sset PCM #{volume}% | #{at_cmd}"&lt;br /&gt;  &lt;br /&gt;  if i == NUM_STEPS&lt;br /&gt;    # Start playing after time is confirmed set.&lt;br /&gt;    alert_time = alert_time + (60)&lt;br /&gt;    at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"&lt;br /&gt;    &lt;br /&gt;    if mp3dir != nil&lt;br /&gt;      run "echo audacious #{mp3dir} | #{at_cmd}"&lt;br /&gt;    else&lt;br /&gt;      run "echo audtool playback-play | #{at_cmd}"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 12 Feb 2007 21:21:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3498</guid>
      <author>bhtek (Boon Hian Tek)</author>
    </item>
  </channel>
</rss>
